java list 多条件排序_List<Map> 多字段排序

package com.example.test;

import com.beust.jcommander.internal.Lists;

import com.beust.jcommander.internal.Maps;

import com.example.utils.DateUtil;

import org.junit.Test;

import java.util.*;

import java.util.stream.Collectors;

/**

* @author

* @Description

* @create 2020-07-29 17:58

*/

public class CommonTest {

class PojoTest {

private int num;

private Date date;

public PojoTest(int num, Date date) {

this.num = num;

this.date = date;

}

public int getNum() {

return num;

}

public void setNum(int num) {

this.num = num;

}

public Date getDate() {

return date;

}

public void setDate(Date date) {

this.date = date;

}

@Override

public String toString() {

return "PojoTest{" +

"num=" + num +

", date=" + date +

'}';

}

}

@Test

public void test() {

List pojos = Lists.newArrayList(

new PojoTest(10, DateUtil.formatStringToDate("2020-07-30", "yyyy-MM-dd")),

new PojoTest(10, DateUtil.formatStringToDate("2020-07-29", "yyyy-MM-dd")),

new PojoTest(16, DateUtil.formatStringToDate("2020-07-28", "yyyy-MM-dd")),

new PojoTest(16, DateUtil.formatStringToDate("2020-07-30", "yyyy-MM-dd")),

new PojoTest(20, DateUtil.formatStringToDate("2020-07-31", "yyyy-MM-dd"))

);

System.err.println("===================0======================");

// stream.sorted 类中单个字段排序

List pojoTests = pojos.stream().sorted(Comparator.comparing(PojoTest::getDate)).collect(Collectors.toList());

pojoTests.forEach(System.err::println);

System.err.println("===================1======================");

// stream.sorted 类中单个字段排序

pojoTests = pojos.stream().sorted(Comparator.comparing(PojoTest::getNum)).collect(Collectors.toList());

pojoTests.forEach(System.err::println);

System.err.println("===================2======================");

// stream.sorted 类中多个字段排序

pojoTests = pojos.stream().sorted(Comparator.comparing(PojoTest::getNum).thenComparing(PojoTest::getDate, Comparator.reverseOrder())).collect(Collectors.toList());

pojoTests.forEach(System.err::println);

System.err.println("===================3======================");

// list.sort 类中多个字段排序

pojos.sort(Comparator.comparing(PojoTest::getNum).thenComparing(PojoTest::getDate, Comparator.reverseOrder()));

pojos.forEach(System.err::println);

System.err.println("===================4======================");

List maps = Lists.newArrayList(

Maps.newHashMap("aa", 10, "bb", DateUtil.formatStringToDate("2020-07-30", "yyyy-MM-dd")),

Maps.newHashMap("aa", 10, "bb", DateUtil.formatStringToDate("2020-07-29", "yyyy-MM-dd")),

Maps.newHashMap("aa", 16, "bb", DateUtil.formatStringToDate("2020-07-28", "yyyy-MM-dd")),

Maps.newHashMap("aa", 16, "bb", DateUtil.formatStringToDate("2020-07-30", "yyyy-MM-dd")),

Maps.newHashMap("aa", 20, "bb", DateUtil.formatStringToDate("2020-07-31", "yyyy-MM-dd"))

);

// List 使用sorted排序时,单个字段排序没有问题, 但是使用thenComparing排序多个字段就会出现问题,似乎是无法确认类型的问题,详细原因我也不是太清楚,有大佬知道的话麻烦可以在评论里说下,谢谢

maps = maps.stream().sorted(Comparator.comparing(a -> Long.parseLong(a.get("aa") + ""))).collect(Collectors.toList());

maps.forEach(System.err::println);

System.err.println("===================5======================");

// maps = maps.stream().sorted(Comparator.comparing(a -> Long.parseLong(a.get("aa") + "")).thenComparing(Comparator.comparing(a -> (Date) a.get("bb"), Comparator.reverseOrder()))).collect(Collectors.toList());

// 上面那种方式没法排序,但是直接使用list的sort排序却是可以的

Comparator aa = Comparator.comparing(a -> Long.parseLong(a.get("aa") + ""));

Comparator bb = Comparator.comparing(a -> (Date) a.get("bb"), Comparator.reverseOrder());

maps.sort(aa.thenComparing(bb));

maps.forEach(System.err::println);

}

}

你可能感兴趣的:(java,list,多条件排序)