Java对中文首字母排序,多条件排序

上代码:


        List<Map<String, String>> userList = new ArrayList<>();

        Map user = new HashMap();
        user.put("name", "张三#1");
        user.put("address", "北极");
        userList.add(user);

        Map user2 = new HashMap();
        user2.put("name", "张三#3");
        user2.put("address", "北极");
        userList.add(user2);

        Map user3 = new HashMap();
        user3.put("name", "张三#2");
        user3.put("address", "阿拉德");
        userList.add(user3);

        Map user4 = new HashMap();
        user4.put("name", "张三#4");
        user4.put("address", "阿拉德");
        userList.add(user4);
        

先把地址按照首字母排序:

        userList.sort((v1, v2) -> Collator.getInstance(Locale.CHINESE).compare(v1.get("address"), v2.get("address")));
        System.out.println(userList);

结果:

[{address=阿拉德, name=张三#2}, {address=阿拉德, name=张三#4}, {address=北极, name=张三#1}, {address=北极, name=张三#3}]

再按照(姓名 + 地址)组合排序:

        userList.sort((v1, v2) -> v1.get("name").toString().compareTo(v2.get("name").toString()));
        userList.sort((v1, v2) -> Collator.getInstance(Locale.CHINESE).compare(v1.get("address"), v2.get("address")));
        System.out.println(userList);

结果:

[{address=阿拉德, name=张三#2}, {address=阿拉德, name=张三#4}, {address=北极, name=张三#1}, {address=北极, name=张三#3}]

你可能感兴趣的:(Java,java,排序算法,开发语言)