当前位置:网站首页>Sort list tool class, which can sort strings
Sort list tool class, which can sort strings
2022-07-04 06:05:00 【Hello song Xianshen】
package com.qpyx.common.utils; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; /** * @Auther: SJH * @Date: 2022/4/22 10:05 * @Description: list Sort by any attribute of the object */ public class SortFildOrderUtile { public static final String DESC = "desc"; public static final String ASC = "asc"; /** * Yes list The elements in are arranged in ascending order . * * @param list * Sort set * @param field * Sort field * @return */ public static List<?> sort(List<?> list, final String field) { return sort(list, field, null); } /** * Yes list The elements in are sorted . * * @param list * Sort set * @param field * Sort field * @param sort * sort order : SortListUtil.DESC( Descending ) SortListUtil.ASC( Ascending ). * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static List<?> sort(List<?> list, final String field, final String sort) { Collections.sort(list, new Comparator() { @Override public int compare(Object a, Object b) { int ret = 0; try { Field f = a.getClass().getDeclaredField(field); f.setAccessible(true); Class<?> type = f.getType(); if (type == int.class) { ret = ((Integer) f.getInt(a)).compareTo((Integer) f .getInt(b)); } else if (type == double.class) { ret = ((Double) f.getDouble(a)).compareTo((Double) f .getDouble(b)); } else if (type == long.class) { ret = ((Long) f.getLong(a)).compareTo((Long) f .getLong(b)); } else if (type == float.class) { ret = ((Float) f.getFloat(a)).compareTo((Float) f .getFloat(b)); } else if (type == Date.class) { ret = ((Date) f.get(a)).compareTo((Date) f.get(b)); } else if (isImplementsOf(type, Comparable.class)) { ret = ((Comparable) f.get(a)).compareTo((Comparable) f .get(b)); } else { ret = String.valueOf(f.get(a)).compareTo( String.valueOf(f.get(b))); } } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (sort != null && sort.toLowerCase().equals(DESC)) { return -ret; } else { return ret; } } }); return list; } /** * Yes list The elements in press fields and sorts Sort , * fields[i] Specify sort fields ,sorts[i] Specify sort method . If sorts[i] If it is blank, it will be arranged in ascending order by default . * * @param list * Sort set * @param fields * Sort field - Array ( One or more ) * @param sorts * Sort rule - Array ( One or more ) * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static List<?> sort(List<?> list, String[] fields, String[] sorts) { if (fields != null && fields.length > 0) { for (int i = fields.length - 1; i >= 0; i--) { final String field = fields[i]; String tmpSort = ASC; if (sorts != null && sorts.length > i && sorts[i] != null) { tmpSort = sorts[i]; } final String sort = tmpSort; Collections.sort(list, new Comparator() { @Override public int compare(Object a, Object b) { int ret = 0; try { Field f = a.getClass().getDeclaredField(field); f.setAccessible(true); Class<?> type = f.getType(); if (type == int.class) { ret = ((Integer) f.getInt(a)) .compareTo((Integer) f.getInt(b)); } else if (type == double.class) { ret = ((Double) f.getDouble(a)) .compareTo((Double) f.getDouble(b)); } else if (type == long.class) { ret = ((Long) f.getLong(a)).compareTo((Long) f .getLong(b)); } else if (type == float.class) { ret = ((Float) f.getFloat(a)) .compareTo((Float) f.getFloat(b)); } else if (type == Date.class) { ret = ((Date) f.get(a)).compareTo((Date) f .get(b)); } else if (isImplementsOf(type, Comparable.class)) { ret = ((Comparable) f.get(a)) .compareTo((Comparable) f.get(b)); } else { ret = String.valueOf(f.get(a)).compareTo( String.valueOf(f.get(b))); } } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (sort != null && sort.toLowerCase().equals(DESC)) { return -ret; } else { return ret; } } }); } } return list; } /** * Judge whether all interfaces implemented by the object contain szInterface * * @param clazz * @param szInterface * @return */ public static boolean isImplementsOf(Class<?> clazz, Class<?> szInterface) { boolean flag = false; Class<?>[] face = clazz.getInterfaces(); for (Class<?> c : face) { if (c == szInterface) { flag = true; } else { flag = isImplementsOf(c, szInterface); } } if (!flag && null != clazz.getSuperclass()) { return isImplementsOf(clazz.getSuperclass(), szInterface); } return flag; } }
边栏推荐
- Online shrimp music will be closed in January next year. Netizens call No
- Input displays the currently selected picture
- fastjson
- How to implement lazy loading in El select (with search function)
- Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
- How to solve the component conflicts caused by scrollbars in GridView
- How to implement cross domain requests
- BeanFactoryPostProcessor 与 BeanPostProcessor 相关子类概述
- 测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文
- The end of the Internet is rural revitalization
猜你喜欢
QT qtablewidget table column top requirements ideas and codes
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
ANSYS command
【微服务】Nacos集群搭建以及加载文件配置
冲击继电器JC-7/11/DC110V
198. House raiding
检漏继电器JY82-2P
Kubernets first meeting
JS arguments parameter usage and explanation
How to expand all collapse panels
随机推荐
Leetcode question brushing record | 206_ Reverse linked list
QT QTableWidget 表格列置顶需求的思路和代码
Luogu deep foundation part 1 Introduction to language Chapter 5 array and data batch storage
【无标题】
Recommended system 1 --- framework
High performance parallel programming and optimization | lesson 02 homework at home
JS how to convert seconds into hours, minutes and seconds display
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
gslb(global server load balance)技术的一点理解
QT get random color value and set label background color code
C语言练习题(递归)
transformer坑了多少算力
体验碎周报第 102 期(2022.7.4)
Nexus 6p从8.0降级6.0+root
Notes and notes
One click filtering to select Baidu online disk files
How does apscheduler set tasks not to be concurrent (that is, execute the next task after the first one)?
我的NVIDIA开发者之旅——优化显卡性能
如何避免 JVM 内存泄漏?
Install pytoch geometric