List集合String字符串按照汉语拼音排序

public static void main(String[] args) {
        List<Map<String, Object>> mapList = new ArrayList<Map<String,Object>>();
        Map<String, Object> m1 = new HashMap<String,Object>();
        m1.put("name", "AC");
        Map<String, Object> m2 = new HashMap<String,Object>();
        m2.put("name", "AB");
        Map<String, Object> m3 = new HashMap<String,Object>();
        m3.put("name", "中国");
        Map<String, Object> m4 = new HashMap<String,Object>();
        m4.put("name", "广东");
        Map<String, Object> m5 = new HashMap<String,Object>();
        m5.put("name", "B东");
        Map<String, Object> m6 = new HashMap<String,Object>();
        m6.put("name", "C东");
        mapList.add(m1);
        mapList.add(m2);
        mapList.add(m3);
        mapList.add(m4);
        mapList.add(m5);
        mapList.add(m6);
        if(CollectionUtils.isNotEmpty(mapList)){
            Collections.sort(mapList, new Comparator<Map<String,Object>>() {  
                @Override  
                public int compare(Map<String,Object> m1, Map<String,Object> m2) {  
                    String o1 = (String) m1.get("name");
                    String o2 = (String) m2.get("name");
                    char c1 = ((String) o1).charAt(0);  
                    char c2 = ((String) o2).charAt(0);  
                    return concatPinyinStringArray(PinyinHelper.toHanyuPinyinStringArray(c1)).compareTo(  
                            concatPinyinStringArray(PinyinHelper.toHanyuPinyinStringArray(c2))); 
                }  
            });  
        }
        
        for (Map<String, Object> map : mapList) {
            System.out.println("map:"+(String)map.get("name"));
        }
    }
    
    
    
    private static String concatPinyinStringArray(String[] pinyinArray) {  
        StringBuffer pinyinSbf = new StringBuffer();  
        if ((pinyinArray != null) && (pinyinArray.length > 0)) {  
            for (int i = 0; i < pinyinArray.length; i++) {  
                pinyinSbf.append(pinyinArray[i]);  
            }  
        }  
        return pinyinSbf.toString();  
    }  

 

posted @ 2019-05-14 11:28  Mr_伍先生  阅读(2585)  评论(0编辑  收藏  举报