当前位置:网站首页>对List进行排序工具类,可以对字符串排序
对List进行排序工具类,可以对字符串排序
2022-07-04 06:00:00 【hello宋先深】
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根据对象任意属性排序 */ public class SortFildOrderUtile { public static final String DESC = "desc"; public static final String ASC = "asc"; /** * 对list中的元素按升序排列. * * @param list * 排序集合 * @param field * 排序字段 * @return */ public static List<?> sort(List<?> list, final String field) { return sort(list, field, null); } /** * 对list中的元素进行排序. * * @param list * 排序集合 * @param field * 排序字段 * @param sort * 排序方式: SortListUtil.DESC(降序) SortListUtil.ASC(升序). * @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; } /** * 对list中的元素按fields和sorts进行排序, * fields[i]指定排序字段,sorts[i]指定排序方式.如果sorts[i]为空则默认按升序排列. * * @param list * 排序集合 * @param fields * 排序字段-数组(一个或多个) * @param sorts * 排序规则-数组(一个或多个) * @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; } /** * 判断对象实现的所有接口中是否包含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; } }
边栏推荐
- webrtc 快速搭建 视频通话 视频会议
- Yiwen unlocks Huawei's new cloud skills - the whole process of aiot development [device access - ESP end-to-side data collection [mqtt]- real time data analysis] (step-by-step screenshot is more detai
- QT releases multilingual International Translation
- How to configure static IP for Kali virtual machine
- 体验碎周报第 102 期(2022.7.4)
- [Chongqing Guangdong education] electronic circuit homework question bank of RTVU secondary school
- QT qtablewidget table column top requirements ideas and codes
- js如何将秒转换成时分秒显示
- Practical gadget instructions
- HMS v1.0 appointment.php editid参数 SQL注入漏洞(CVE-2022-25491)
猜你喜欢
webrtc 快速搭建 视频通话 视频会议
Detailed explanation of common APIs for component and container containers: frame, panel, scrollpane
[openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx
How to expand all collapse panels
VB. Net GIF (making and disassembling - optimizing code, class library - 5)
How to solve the component conflicts caused by scrollbars in GridView
Take you to quickly learn how to use qsort and simulate qsort
C语言中的函数(详解)
Gridview出现滚动条,组件冲突,如何解决
What are the reasons for the frequent high CPU of ECS?
随机推荐
AWT introduction
Install pytoch geometric
left_and_right_net可解释性设计
Design and implementation of redis 7.0 multi part AOF
Descriptive analysis of data distribution characteristics (data exploration)
VB. Net GIF (making and disassembling - optimizing code, class library - 5)
BUU-Crypto-[HDCTF2019]basic rsa
JSON web token -- comparison between JWT and traditional session login authentication
Input displays the currently selected picture
剑指 Offer II 038. 每日温度
Leetcode question brushing record | 206_ Reverse linked list
Compound nonlinear feedback control (2)
"In simple language programming competition (basic)" part 1 Introduction to language Chapter 3 branch structure programming
Win10 clear quick access - leave no trace
How to determine whether an array contains an element
BUU-Pwn-test_ your_ nc
Notes and notes
gslb(global server load balance)技术的一点理解
The end of the Internet is rural revitalization
JS arguments parameter usage and explanation