当前位置:   article > 正文

Java对list动态排序_java对list 动态排序

java对list 动态排序

期望:动态的根据某个属性进行排序

目录

 方案一:

1.创建User对象

2.实现自定义排序工具类

3.test

4.输出结果展示

方案二:

1.创建User对象

2.实现自定义排序工具类

3.test

4.输出结果展示


 方案一:

1.创建User对象

  1. @Data
  2. public class User {
  3. private Integer id;
  4. private String name;
  5. private Integer age;
  6. }

2.实现自定义排序工具类

MyCompare
  1. @Data
  2. public class MyCompare<T> implements Comparator<T> {
  3. private String param;
  4. private Boolean flag;
  5. private Comparator<Object> comparator;
  6. public MyCompare(String param, Boolean flag) {
  7. this.setParam(param);
  8. this.setFlag(flag);
  9. this.comparator = new RealCompare<>();
  10. }
  11. @Override
  12. public int compare(T o1, T o2) {
  13. try {
  14. Object value1 = PropertyUtils.getProperty(o1, this.param);
  15. Object value2 = PropertyUtils.getProperty(o2, this.param);
  16. if (value1 == null) {
  17. return 1;
  18. }
  19. if (value2 == null) {
  20. return -1;
  21. }
  22. Comparator<Object> comparator = this.comparator;
  23. return flag ? comparator.compare(value1, value2) : -comparator.compare(value1, value2);
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. return 0;
  28. }
  29. }
RealCompare
  1. public class RealCompare<T> implements Comparator<T> {
  2. @Override
  3. public int compare(Object obj1, Object obj2) {
  4. if (obj1 == null) {
  5. return 1;
  6. }
  7. if (obj2 == null) {
  8. return -1;
  9. }
  10. return (int)obj1 - (int)obj2;
  11. }
  12. }

3.test

  1. public static void main(String[] args) {
  2. User u1 = new User();
  3. u1.setId(10);
  4. u1.setName("aaa");
  5. u1.setAge(18);
  6. User u3 = new User();
  7. // u3.setId(12);
  8. u3.setName("bbb");
  9. u3.setAge(20);
  10. User u2 = new User();
  11. u2.setId(11);
  12. u2.setName("ccc");
  13. u2.setAge(19);
  14. List<User> list = new ArrayList<>();
  15. list.add(u1);
  16. list.add(u2);
  17. list.add(u3);
  18. //按照id进行排序,false表示降序
  19. List<User> sortList = list.stream().sorted(new MyCompare<>("id",false)).collect(Collectors.toList());
  20. for (User user : id) {
  21. System.out.println(user);
  22. }
  23. }

4.输出结果展示

方案二:

1.创建User对象

  1. @Data
  2. public class User {
  3. private Integer id;
  4. private String name;
  5. private Integer age;
  6. }

2.实现自定义排序工具类

  1. public class ListSortUtil {
  2. /**
  3. * 自定义字段排序
  4. *
  5. * @param list
  6. * @param sortName
  7. * @param desc
  8. * @param descNull
  9. * @return
  10. */
  11. public static <T> List<T> sortListByField(List<T> list, String sortName, boolean desc, boolean descNull, Class<T> tClass) {
  12. return list.stream()
  13. .map(convertBeanToMap())
  14. .sorted(getMapComparator(sortName, desc, descNull))
  15. .map(convertMapToBean(tClass))
  16. .collect(Collectors.toList());
  17. }
  18. public static <T> List<?> sortListByField(List<T> list, String sortName, boolean desc, Class<T> tClass) {
  19. return sortListByField(list, sortName, desc, true, tClass);
  20. }
  21. private static Comparator<Map<String, Object>> getMapComparator(String sortName, boolean desc, boolean descNull) {
  22. if (desc) {
  23. return ((Comparator<Map<String, Object>>) (o1, o2) -> getCompareUtil(sortName, descNull, o1, o2)).reversed();
  24. }
  25. return (o1, o2) -> getCompareUtil(sortName, descNull, o1, o2);
  26. }
  27. /**
  28. * 比较方法
  29. *
  30. * @param sortName 排序字段
  31. * @param descNull null值排序规则
  32. * @param o1
  33. * @param o2
  34. * @return
  35. */
  36. private static int getCompareUtil(String sortName, boolean descNull, Map<String, Object> o1, Map<String, Object> o2) {
  37. return CompareUtil.compare(o1.get(sortName), o2.get(sortName), descNull);
  38. }
  39. private static <T> Function<Map<String, Object>, T> convertMapToBean(Class<T> tClass) {
  40. return map -> BeanUtil.toBean(map, tClass);
  41. }
  42. private static <T> Function<T, Map<String, Object>> convertBeanToMap() {
  43. return BeanUtil::beanToMap;
  44. }
  45. }

3.test

  1. public static void main(String[] args) {
  2. User u1 = new User();
  3. u1.setId(10);
  4. u1.setName("aaa");
  5. u1.setAge(18);
  6. User u3 = new User();
  7. // u3.setId(12);
  8. u3.setName("bbb");
  9. u3.setAge(20);
  10. User u2 = new User();
  11. u2.setId(11);
  12. u2.setName("ccc");
  13. u2.setAge(19);
  14. List<User> list = new ArrayList<>();
  15. list.add(u1);
  16. list.add(u2);
  17. list.add(u3);
  18. //按照id进行排序,desc为false表示升序,descNull为false表示null值排到最后面
  19. List<User> id = ListSortUtil.sortListByField(list, "id", false, false, User.class);
  20. for (User user : id) {
  21. System.out.println(user);
  22. }
  23. }

4.输出结果展示

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/557975
推荐阅读
相关标签
  

闽ICP备14008679号