原 lambda表达式对list中的中文进行排序
版权声明:本文为博主原创文章,请尊重他人的劳动成果,转载请附上原文出处链接和本声明。
本文链接:https://www.91mszl.com/zhangwuji/article/details/1429
需求:我们对list中的username字段进行排序,要求按照中文的首字母进行排序
一:代码如下
public static void main(String[] args) {
List<User> dataList=new ArrayList<>();
User u1=new User();
u1.setUserName("张无忌");
u1.setMobile("13248254444");
User u2=new User();
u2.setUserName("阿三");
u2.setMobile("13248254444");
User u3=new User();
u3.setUserName("曹操");
u3.setMobile("13248254444");
dataList.add(u1);
dataList.add(u2);
dataList.add(u3);
System.out.println("排序前:" + dataList);
Comparator<Object> chinese=Collator.getInstance(Locale.CHINA);
Collections.sort(dataList, (v1, v2) ->
chinese.compare(v1.getUserName(), v2.getUserName())
);
System.out.println("排序后:" + dataList);
}
执行结果:
排序前:[User(userId=null, userName=张无忌, nickName=null, password=null, email=null, mobile=13248254444, position=null, dept=null, avatarUrl=null, status=null, customerType=null, createTime=null, updateTime=null), User(userId=null, userName=阿三, nickName=null, password=null, email=null, mobile=13248254444, position=null, dept=null, avatarUrl=null, status=null, customerType=null, createTime=null, updateTime=null), User(userId=null, userName=曹操, nickName=null, password=null, email=null, mobile=13248254444, position=null, dept=null, avatarUrl=null, status=null, customerType=null, createTime=null, updateTime=null)]
排序后:[User(userId=null, userName=阿三, nickName=null, password=null, email=null, mobile=13248254444, position=null, dept=null, avatarUrl=null, status=null, customerType=null, createTime=null, updateTime=null), User(userId=null, userName=曹操, nickName=null, password=null, email=null, mobile=13248254444, position=null, dept=null, avatarUrl=null, status=null, customerType=null, createTime=null, updateTime=null), User(userId=null, userName=张无忌, nickName=null, password=null, email=null, mobile=13248254444, position=null, dept=null, avatarUrl=null, status=null, customerType=null, createTime=null, updateTime=null)]
2023-03-27 09:22:34 阅读(507)
名师出品,必属精品 https://www.91mszl.com
博主信息