java按照中文首字母排序

     
//这是按照list里面的某个属性排序 Collections
     Comparator comparator = Collator.getInstance(java.util.Locale.CHINA); BaseWebsiteVO a1 = BaseWebsiteVO.builder().name("测试").build(); BaseWebsiteVO a2 = BaseWebsiteVO.builder().name("百度").build(); BaseWebsiteVO a3 = BaseWebsiteVO.builder().name("搜狐").build(); List<BaseWebsiteVO> baseWebsiteVOS = Arrays.asList(a1, a2, a3); Collections.sort(baseWebsiteVOS, (o1, o2) -> comparator.compare(o1.getName(), o2.getName())); baseWebsiteVOS.forEach(i -> System.out.println(i.getName()));
//这个按照数组排序 String [] arr
= {"测试","百度","搜狐"}; Arrays.sort(arr,comparator); for (String s : arr) { System.out.println(s); }

 

posted @ 2022-05-30 09:30  xxfcode  阅读(715)  评论(0编辑  收藏  举报