本文主要是介绍Guava排序-Guava实现多列排序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Guava排序:ComparisonChain
这里我先说一下我的一个需求,然后对照着需求来说一下如何实现对应的逻辑。
首先我有一个List
// 姓名,性别,年龄,薪资,级别,籍贯
List<List<Object>> lists = Lists.newArrayList();lists.add(Arrays.asList("张三", "男", 22, 10000, "T2", "贵州遵义"));
lists.add(Arrays.asList("李四", "女", 23, 11000, "T2", "贵州遵义"));
lists.add(Arrays.asList("王五", "女", 23, 12000, "T3", "北京海淀"));
lists.add(Arrays.asList("王六", "男", 23, 13000, "T3", "北京昌平"));
lists.add(Arrays.asList("王七", "男", 24, 14000, "T3", "北京昌平"));
lists.add(Arrays.asList("王八", "女", 23, 12000, "T2", "北京昌平"));
lists.add(Arrays.asList("胡三", "男", 26, 12000, "T3", "北京朝阳"));
lists.add(Arrays.asList("胡四", "男", 26, 13000, "T3", "北京朝阳"));
lists.add(Arrays.asList("张五", "女", 26, 14000, "T3", "北京海淀"));
lists.add(Arrays.asList("张六", "男", 27, 17000, "T4", "北京朝阳"));
lists.add(Arrays.asList("张七", "男", 23, 12000, "T3", "北京朝阳"));
lists.add(Arrays.asList("黄五", "女", 24, 11000, "T2", "北京海淀"));
lists.add(Arrays.asList("黄三", "男", 24, 10000, "T2", "北京大兴"));
lists.add(Arrays.asList("刘爱", "男", 22, 9000, "T1", "北京大兴"));
lists.add(Arrays.asList("刘跟", "男", 27, 18000, "T4", "贵州遵义"));
lists.add(Arrays.asList("李根", "男", 28, 20000, "T5", "贵州贵阳"));
lists.add(Arrays.asList("郭艾琳", "男", 24, 12000, "T3", "贵州贵阳"));
现在我想要对这些数据进行一次排序,根据年龄、薪资和级别来排序。也就是说对三列进行排序,如果数据存在库里面,那肯定是可以直接order by,但是我现在的数据是在内存里面,如何排序呢?
这里就可以使用到了Guava的ComparisonChain,ComparisonChain是一个链式排序逻辑,把需要排序的列先后往ComparisonChain中添加即可,主要的代码:
...
public abstract <T> ComparisonChain compare(@NullableDecl T var1, @NullableDecl T var2, Comparator<T> var3);
...
排序实例:
private static Comparator<List<Object>> listComparator(Map<Integer, Boolean> colSortMaps) {Ordering ordering = Ordering.natural();return (list1, list2) -> {ComparisonChain compareChain = ComparisonChain.start();for (Integer index : colSortMaps.keySet()) {Object currObj = Optional.ofNullable(list1.get(index)).orElse("");Object compObj = Optional.ofNullable(list2.get(index)).orElse("");Comparator<Object> objComparator = objComparator(ordering, colSortMaps.getOrDefault(index, true));compareChain = compareChain.compare(currObj, compObj, objComparator);}return compareChain.result();};}private static Comparator<Object> objComparator(Ordering ordering, boolean asc) {if (asc) {return ordering::compare;} else {return ordering.reverse()::compare;}
}
其中Map colSortMaps这个参数表示列序号和是否升序的Map,调用排序:
Map<Integer, Boolean> colSortMaps = Maps.newHashMap();
colSortMaps.put(2, true); // 年龄升序
colSortMaps.put(3, true); // 薪资升序
colSortMaps.put(4, true); // 级别升序lists.sort(listComparator(colSortMaps));
排序结果:
刘爱,男,22,9000,T1,北京大兴
张三,男,22,10000,T2,贵州遵义
李四,女,23,11000,T2,贵州遵义
王八,女,23,12000,T2,北京昌平
王五,女,23,12000,T3,北京海淀
张七,男,23,12000,T3,北京朝阳
王六,男,23,13000,T3,北京昌平
黄三,男,24,10000,T2,北京大兴
黄五,女,24,11000,T2,北京海淀
郭艾琳,男,24,12000,T3,贵州贵阳
王七,男,24,14000,T3,北京昌平
胡三,男,26,12000,T3,北京朝阳
胡四,男,26,13000,T3,北京朝阳
张五,女,26,14000,T3,北京海淀
张六,男,27,17000,T4,北京朝阳
刘跟,男,27,18000,T4,贵州遵义
李根,男,28,20000,T5,贵州贵阳
Guava是一个Java神库,源码也写的非常好,多看Guava的源码也使我收获了不少,希望交流,多多进步。
这篇关于Guava排序-Guava实现多列排序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!