Thursday, 19 June 2014

Collection.sort Comparator and Collator - compare special chars or just polish words

Here is the short example code.

 Collections.sort(parse, new Comparator<HashMap<String, String>>() {  
           @Override  
           public int compare(HashMap<String, String> lhs, HashMap<String, String> rhs) {  
             String station1 = null;  
             String station2 = null;  
   
             try {  
               station1 = new String(lhs.get("station").getBytes(), "utf-8");  
               station2 = new String(rhs.get("station").getBytes(), "utf-8");  
             } catch (UnsupportedEncodingException e) {  
               e.printStackTrace();  
             }  
   
             Locale locale = new Locale("pl_PL");  
             Collator collator = Collator.getInstance(locale);  
             return collator.compare(station1, station2);  
           }  
         });