赞
踩
目录
- @Data
- public class User {
-
- private Integer id;
-
- private String name;
-
- private Integer age;
-
-
- }
MyCompare
- @Data
- public class MyCompare<T> implements Comparator<T> {
-
- private String param;
-
- private Boolean flag;
-
- private Comparator<Object> comparator;
-
- public MyCompare(String param, Boolean flag) {
- this.setParam(param);
- this.setFlag(flag);
- this.comparator = new RealCompare<>();
- }
-
- @Override
- public int compare(T o1, T o2) {
- try {
- Object value1 = PropertyUtils.getProperty(o1, this.param);
- Object value2 = PropertyUtils.getProperty(o2, this.param);
-
- if (value1 == null) {
- return 1;
- }
- if (value2 == null) {
- return -1;
- }
-
- Comparator<Object> comparator = this.comparator;
- return flag ? comparator.compare(value1, value2) : -comparator.compare(value1, value2);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return 0;
- }
- }
RealCompare
- public class RealCompare<T> implements Comparator<T> {
-
- @Override
- public int compare(Object obj1, Object obj2) {
- if (obj1 == null) {
- return 1;
- }
- if (obj2 == null) {
- return -1;
- }
- return (int)obj1 - (int)obj2;
- }
- }
- public static void main(String[] args) {
- User u1 = new User();
- u1.setId(10);
- u1.setName("aaa");
- u1.setAge(18);
- User u3 = new User();
- // u3.setId(12);
- u3.setName("bbb");
- u3.setAge(20);
- User u2 = new User();
- u2.setId(11);
- u2.setName("ccc");
- u2.setAge(19);
-
- List<User> list = new ArrayList<>();
- list.add(u1);
- list.add(u2);
- list.add(u3);
-
- //按照id进行排序,false表示降序
- List<User> sortList = list.stream().sorted(new MyCompare<>("id",false)).collect(Collectors.toList());
- for (User user : id) {
- System.out.println(user);
- }
-
- }
- @Data
- public class User {
-
- private Integer id;
-
- private String name;
-
- private Integer age;
-
-
- }
- public class ListSortUtil {
-
- /**
- * 自定义字段排序
- *
- * @param list
- * @param sortName
- * @param desc
- * @param descNull
- * @return
- */
- public static <T> List<T> sortListByField(List<T> list, String sortName, boolean desc, boolean descNull, Class<T> tClass) {
- return list.stream()
- .map(convertBeanToMap())
- .sorted(getMapComparator(sortName, desc, descNull))
- .map(convertMapToBean(tClass))
- .collect(Collectors.toList());
- }
-
- public static <T> List<?> sortListByField(List<T> list, String sortName, boolean desc, Class<T> tClass) {
- return sortListByField(list, sortName, desc, true, tClass);
- }
-
- private static Comparator<Map<String, Object>> getMapComparator(String sortName, boolean desc, boolean descNull) {
- if (desc) {
- return ((Comparator<Map<String, Object>>) (o1, o2) -> getCompareUtil(sortName, descNull, o1, o2)).reversed();
- }
- return (o1, o2) -> getCompareUtil(sortName, descNull, o1, o2);
- }
-
- /**
- * 比较方法
- *
- * @param sortName 排序字段
- * @param descNull null值排序规则
- * @param o1
- * @param o2
- * @return
- */
- private static int getCompareUtil(String sortName, boolean descNull, Map<String, Object> o1, Map<String, Object> o2) {
- return CompareUtil.compare(o1.get(sortName), o2.get(sortName), descNull);
- }
-
-
- private static <T> Function<Map<String, Object>, T> convertMapToBean(Class<T> tClass) {
- return map -> BeanUtil.toBean(map, tClass);
- }
-
- private static <T> Function<T, Map<String, Object>> convertBeanToMap() {
- return BeanUtil::beanToMap;
- }
- }
- public static void main(String[] args) {
- User u1 = new User();
- u1.setId(10);
- u1.setName("aaa");
- u1.setAge(18);
- User u3 = new User();
- // u3.setId(12);
- u3.setName("bbb");
- u3.setAge(20);
- User u2 = new User();
- u2.setId(11);
- u2.setName("ccc");
- u2.setAge(19);
-
- List<User> list = new ArrayList<>();
- list.add(u1);
- list.add(u2);
- list.add(u3);
-
- //按照id进行排序,desc为false表示升序,descNull为false表示null值排到最后面
- List<User> id = ListSortUtil.sortListByField(list, "id", false, false, User.class);
-
- for (User user : id) {
- System.out.println(user);
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。