当前位置:网站首页>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; } }
边栏推荐
- 实用的小工具指令
- ANSYS command
- Canoe panel learning video
- How to implement cross domain requests
- 2022.7.2-----leetcode. eight hundred and seventy-one
- Qt发布多语言国际化翻译
- LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout
- Design and implementation of redis 7.0 multi part AOF
- QT 获取随机颜色值设置label背景色 代码
- Recommended system 1 --- framework
猜你喜欢
随机推荐
JSON Web Token----JWT和传统session登录认证对比
"In simple language programming competition (basic)" part 1 Introduction to language Chapter 3 branch structure programming
Experience weekly report no. 102 (July 4, 2022)
509. 斐波那契数、爬楼梯所有路径、爬楼梯最小花费
JS how to convert seconds into hours, minutes and seconds display
剑指 Offer II 038. 每日温度
px em rem的区别
Qt发布多语言国际化翻译
Take you to quickly learn how to use qsort and simulate qsort
buuctf-pwn write-ups (8)
Invalid revision: 3.18.1-g262b901-dirty
报错cvc-complex-type.2.4.a: 发现了以元素 ‘base-extension‘ 开头的无效内容。应以 ‘{layoutlib}‘ 之一开头。
实用的小工具指令
Accidentally deleted the data file of Clickhouse, can it be restored?
Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)
Input displays the currently selected picture
Install pytoch geometric
测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文
[Chongqing Guangdong education] electronic circuit homework question bank of RTVU secondary school
Nexus 6p downgraded from 8.0 to 6.0+root