1.你要按拼音排序(字母同音依次比后面字母)
//集合按中文拼音排序Collections.sort(collect,new Comparator() {@Overridepublic int compare(Object o1, Object o2) {return chineseCompare(o1,o2);}});//排序方法private static int chineseCompare(Object obj1,Object obj2){String s1 = (String) obj1;String s2 = (String)obj2;Collator myCollator = Collator.getInstance(java.util.Locale.CHINA);return myCollator.compare(s1, s2);}
2.你要其他顺序且原顺序偏差较大,先stream里sorted一下,再通过swap调整顺序
//成员方法 List<String> collect = baseOrganizeService.list(queryWrapper).stream().map(BaseOrganizeEntity::getFullname).sorted().collect(Collectors.toList());swap(collect,1,2);
}private static <E> void swap(List<E> list, int index1, int index2) {//定义第三方变量E e = list.get(index1);//交换值list.set(index1, list.get(index2));list.set(index2, e);}
我用的第二种,看看效果(先调整好集合顺序再遍历集合去处理数据效率最好):