当前位置:网站首页>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; } }
边栏推荐
- QT releases multilingual International Translation
- 复合非线性反馈控制(二)
- Luogu deep foundation part 1 Introduction to language Chapter 5 array and data batch storage
- 如何获取el-tree中所有节点的父节点
- How to implement cross domain requests
- One click filtering to select Baidu online disk files
- How to implement lazy loading in El select (with search function)
- Input displays the currently selected picture
- Halcon图片标定,使得后续图片处理过后变成与模板图片一样
- Vant --- detailed explanation and use of list component in vant
猜你喜欢
[openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx
JS flattened array of number shape structure
transformer坑了多少算力
AWT常用组件、FileDialog文件选择框
Practical gadget instructions
(4) Canal multi instance use
Configure cross compilation tool chain and environment variables
Programmers don't talk about morality, and use multithreading for Heisi's girlfriend
gslb(global server load balance)技术的一点理解
Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
随机推荐
卸载Google Drive 硬盘-必须退出程序才能卸载
MySQL的information_schema数据库
Error CVC complex type 2.4. a: Invalid content beginning with element 'base extension' was found. Should start with one of '{layoutlib}'.
js获取对象中嵌套的属性值
Grounding relay dd-1/60
el-select如何实现懒加载(带搜索功能)
接地继电器DD-1/60
如何判断数组中是否含有某个元素
198. House raiding
QT releases multilingual International Translation
AWT common components, FileDialog file selection box
One click filtering to select Baidu online disk files
1.1 history of Statistics
Weekly summary (*63): about positive energy
How to get the parent node of all nodes in El tree
2022.7.3-----leetcode.556
JS扁平化数形结构的数组
509. Fibonacci number, all paths of climbing stairs, minimum cost of climbing stairs
Luogu deep foundation part 1 Introduction to language Chapter 5 array and data batch storage
Canoe panel learning video