• 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();  
        }  
  • 相关阅读:
    俄罗斯方块游戏JavaScript代码
    NET工厂模式架构
    MVC之Session State性能
    配置模型详解
    Elasticsearch
    java中处理字符编码(网页与数据库)(转)
    如何设置一个activity透明(转)
    view和activity的区别(转)
    微擎系统搭建(转)
    Java Gradle入门指南之依赖管理(添加依赖、仓库、版本冲突) (转)
  • 原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/10861299.html
Copyright © 2020-2023  润新知