java 按照集合中某一个中文值进行排序

版权声明:本文为博主原创文章,如果转载请务必注明出自本博客:qq_2300688967,否则追究责任。 https://blog.csdn.net/qq_2300688967/article/details/84287746

例如某一个集合如下:如果想按照userName的首字母进行排序,优先级:特殊字符、数字、字母、中文

{
    "code": 200,
    "msg": "成功",
    "result": [
        {
            "id": 923,
            "userName": "test",
            "type": 7
        },
        {
            "id": 926,
            "userName": "仲",
            "type": 7
        },
        {
            "id": 927,
            "userName": "啊",
            "type": 7
        },
        {
            "id": 928,
            "userName": "**好",
            "type": 7
        },
        {
            "id": 929,
            "userName": "#怎么说",
            "type": 7
        },
        {
            "id": 930,
            "userName": "@163.com",
            "type": 7
        },
        {
            "id": 934,
            "userName": "12ze",
            "type": 7
        }
    ]
}

方法代码如下:其中saleUserVOS是保存的其中的对象集合,userName为其中对象的属性 

Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
saleUserVOS.sort((o1, o2) -> ((Collator) com).compare(o1.getUserName(), o2.getUserName()));

排序后返回结果如下:

{
    "code": 200,
    "msg": "成功",
    "result": [
        {
            "id": 930,
            "userName": "@163.com",
            "type": 7
        },
        {
            "id": 928,
            "userName": "**好",
            "type": 7
        },
        {
            "id": 929,
            "userName": "#怎么说",
            "type": 7
        },
        {
            "id": 934,
            "userName": "12ze",
            "type": 7
        },
        {
            "id": 923,
            "userName": "test",
            "type": 7
        },
        {
            "id": 927,
            "userName": "啊",
            "type": 7
        },
        {
            "id": 926,
            "userName": "仲",
            "type": 7
        }
    ]
}

猜你喜欢

转载自blog.csdn.net/qq_2300688967/article/details/84287746