91名师指路-头部
91名师指路

lambda表达式对list中的中文进行排序

由于某些原因,现在不支持支付宝支付,如需要购买源码请加博主微信进行购买,微信号:13248254750

需求:我们对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     阅读(508)

名师出品,必属精品    https://www.91mszl.com

联系博主    
用户登录遮罩层
x

账号登录

91名师指路-底部