lambda表达式对LIST对象多个字段进行排序

Student.java

public class Student {
	private Long id;
	private String name;
	private String age;
	private Date birthday;
 
	public Student() {
	}
 
	public Student(Long id, String name, String age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}
 
	public Student(String name, String age) {
		this.name = name;
		this.age = age;
	}
 
	public Student(String name, String age, Date birthday) {
		this.name = name;
		this.age = age;
		this.birthday = birthday;
	}
 
	public Long getId() {
		return id;
	}
 
	public void setId(Long id) {
		this.id = id;
	}
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	public String getAge() {
		return age;
	}
 
	public void setAge(String age) {
		this.age = age;
	}
 
	public Date getBirthday() {
		return birthday;
	}
 
	public String getBirthdayStr() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(birthday);
	}
 
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
 
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", birthday=" + getBirthdayStr() + "]";
	}
}

Listtest.java

public class ListTest {
	@Test
	public void test() {
		List<Student> stuList = new ArrayList<Student>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		stuList.add(new Student("wang", "18", sdf.parse("2007/04/01")));
		stuList.add(new Student("li", "19", sdf.parse("2007/05/01")));
		stuList.add(new Student("liu", "20", sdf.parse("2006/04/01")));
		stuList.add(new Student("xi", "15", sdf.parse("2007/04/01")));
		stuList.add(new Student("li", "17", sdf.parse("2007/05/01")));
		stuList.add(new Student("wang", "19", sdf.parse("2007/06/01")));
 
		// age升序
		Comparator<Student> byIdASC = Comparator.comparing(Student::getAge);
 
		// named不分区大小写降序
		Comparator<Student> byNameDESC = Comparator.comparing(Student::getName, String.CASE_INSENSITIVE_ORDER)
				.reversed();
 
		// birthday 降序
		Comparator<Student> byBirthdayDESC = Comparator.comparing(Student::getBirthday).reversed();
 
		// 联合排序
		Comparator<Student> finalComparator = byIdASC.thenComparing(byNameDESC).thenComparing(byBirthdayDESC);
 
		List<Student> result = stuList.stream().sorted(finalComparator).collect(Collectors.toList());
		print(result);
		// 输出结果:
		// Student [name=xi, age=15, birthday=2007-04-01]
		// Student [name=li, age=17, birthday=2007-05-01]
		// Student [name=wang, age=18, birthday=2007-04-01]
		// Student [name=wang, age=19, birthday=2007-06-01]
		// Student [name=li, age=19, birthday=2007-05-01]
		// Student [name=liu, age=20, birthday=2006-04-01]
 
	}
}
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值