当前位置:网站首页>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; } }
边栏推荐
- js arguments参数使用和详解
- 注释与注解
- Invalid revision: 3.18.1-g262b901-dirty
- How much computing power does transformer have
- 如何判断数组中是否含有某个元素
- Leetcode question brushing record | 206_ Reverse linked list
- Detailed explanation of common APIs for component and container containers: frame, panel, scrollpane
- 4G wireless all network solar hydrological equipment power monitoring system bms110
- Weekly summary (*63): about positive energy
- Use of hutool Pinyin tool
猜你喜欢
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
509. 斐波那契数、爬楼梯所有路径、爬楼梯最小花费
transformer坑了多少算力
C language exercises (recursion)
Error CVC complex type 2.4. a: Invalid content beginning with element 'base extension' was found. Should start with one of '{layoutlib}'.
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
How to avoid JVM memory leakage?
Overview of relevant subclasses of beanfactorypostprocessor and beanpostprocessor
测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文
ANSYS command
随机推荐
js如何将秒转换成时分秒显示
HMS v1.0 appointment.php editid参数 SQL注入漏洞(CVE-2022-25491)
Descriptive analysis of data distribution characteristics (data exploration)
Win10 clear quick access - leave no trace
Halcon图片标定,使得后续图片处理过后变成与模板图片一样
Notes and notes
Excel 比较日器
How to get the parent node of all nodes in El tree
Json Web token - jwt vs. Traditional session login Authentication
LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout
Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式
Design and implementation of tcp/ip series overview
The difference between PX EM rem
Canoe panel learning video
509. Fibonacci number, all paths of climbing stairs, minimum cost of climbing stairs
实用的小工具指令
JSON Web Token----JWT和传统session登录认证对比
2022.7.3-----leetcode.556
QT releases multilingual International Translation
How to expand all collapse panels