对LIST对象多个字段进行排序

jdk1.8之前的做法

参考: http://blog.csdn.net/enable1234___/article/details/53224740

jdk1.8新特性的做法:

参考: http://blog.csdn.net/aitangyong/article/details/54880228

Student.java

 
  1. public class Student {

  2. private Long id;

  3. private String name;

  4. private String age;

  5. private Date birthday;

  6. public Student() {

  7. }

  8. public Student(Long id, String name, String age) {

  9. this.id = id;

  10. this.name = name;

  11. this.age = age;

  12. }

  13. public Student(String name, String age) {

  14. this.name = name;

  15. this.age = age;

  16. }

  17. public Student(String name, String age, Date birthday) {

  18. this.name = name;

  19. this.age = age;

  20. this.birthday = birthday;

  21. }

  22. public Long getId() {

  23. return id;

  24. }

  25. public void setId(Long id) {

  26. this.id = id;

  27. }

  28. public String getName() {

  29. return name;

  30. }

  31. public void setName(String name) {

  32. this.name = name;

  33. }

  34. public String getAge() {

  35. return age;

  36. }

  37. public void setAge(String age) {

  38. this.age = age;

  39. }

  40. public Date getBirthday() {

  41. return birthday;

  42. }

  43. public String getBirthdayStr() {

  44. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

  45. return sdf.format(birthday);

  46. }

  47. public void setBirthday(Date birthday) {

  48. this.birthday = birthday;

  49. }

  50. @Override

  51. public String toString() {

  52. return "Student [name=" + name + ", age=" + age + ", birthday=" + getBirthdayStr() + "]";

  53. }

  54. }

Listtest.java

 
  1. public class ListTest {

  2. @Test

  3. public void test() {

  4. List<Student> stuList = new ArrayList<Student>();

  5. SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

  6. stuList.add(new Student("wang", "18", sdf.parse("2007/04/01")));

  7. stuList.add(new Student("li", "19", sdf.parse("2007/05/01")));

  8. stuList.add(new Student("liu", "20", sdf.parse("2006/04/01")));

  9. stuList.add(new Student("xi", "15", sdf.parse("2007/04/01")));

  10. stuList.add(new Student("li", "17", sdf.parse("2007/05/01")));

  11. stuList.add(new Student("wang", "19", sdf.parse("2007/06/01")));

  12. // age升序

  13. Comparator<Student> byIdASC = Comparator.comparing(Student::getAge);

  14. // named不分区大小写降序

  15. Comparator<Student> byNameDESC = Comparator.comparing(Student::getName, String.CASE_INSENSITIVE_ORDER)

  16. .reversed();

  17. // birthday 降序

  18. Comparator<Student> byBirthdayDESC = Comparator.comparing(Student::getBirthday).reversed();

  19. // 联合排序

  20. Comparator<Student> finalComparator = byIdASC.thenComparing(byNameDESC).thenComparing(byBirthdayDESC);

  21. List<Student> result = stuList.stream().sorted(finalComparator).collect(Collectors.toList());

  22. print(result);

  23. // 输出结果:

  24. // Student [name=xi, age=15, birthday=2007-04-01]

  25. // Student [name=li, age=17, birthday=2007-05-01]

  26. // Student [name=wang, age=18, birthday=2007-04-01]

  27. // Student [name=wang, age=19, birthday=2007-06-01]

  28. // Student [name=li, age=19, birthday=2007-05-01]

  29. // Student [name=liu, age=20, birthday=2006-04-01]

  30. }

  31. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值