码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-05-14 12:48:47      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:str   rri   help   arraylist   any   ide   div   拼音   cti   

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();  
    }  

 

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

标签:str   rri   help   arraylist   any   ide   div   拼音   cti   

原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/10861299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!