List集合按照对象中某个属性排序通用工具类(按对象中某属性排序)
来源:互联网 发布:2016年保险业数据 编辑:程序博客网 时间:2024/04/29 15:21
在开发中,我们可能会遇到,我们会根据不同情况对一个List集合进行排序操作,为了方便List集合根据对象中的某一个属性进行排序,开发了如下工具类:
package test;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.Collections;import java.util.Comparator;import java.util.Date;import java.util.List;/** * admin<br> * 通用工具类之按对象中某属性排序 * */public class SortListUtil {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() {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() {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;}/** * 默认按正序排列 * * @param list * 排序集合 * @param method * 排序方法 "getXxx()" * @return */public static List<?> sortByMethod(List<?> list, final String method) {return sortByMethod(list, method, null);}/** * 集合排序 * * @param list * 排序集合 * @param method * 排序方法 "getXxx()" * @param sort * 排序方式: SortListUtil.DESC(降序) SortListUtil.ASC(升序). * @return */@SuppressWarnings({ "unchecked", "rawtypes" })public static List<?> sortByMethod(List<?> list, final String method,final String sort) {Collections.sort(list, new Comparator() {public int compare(Object a, Object b) {int ret = 0;try {Method m = a.getClass().getMethod(method, null);m.setAccessible(true);Class<?> type = m.getReturnType();if (type == int.class) {ret = ((Integer) m.invoke(a, null)).compareTo((Integer) m.invoke(b, null));} else if (type == double.class) {ret = ((Double) m.invoke(a, null)).compareTo((Double) m.invoke(b, null));} else if (type == long.class) {ret = ((Long) m.invoke(a, null)).compareTo((Long) m.invoke(b, null));} else if (type == float.class) {ret = ((Float) m.invoke(a, null)).compareTo((Float) m.invoke(b, null));} else if (type == Date.class) {ret = ((Date) m.invoke(a, null)).compareTo((Date) m.invoke(b, null));} else if (isImplementsOf(type, Comparable.class)) {ret = ((Comparable) m.invoke(a, null)).compareTo((Comparable) m.invoke(b, null));} else {ret = String.valueOf(m.invoke(a, null)).compareTo(String.valueOf(m.invoke(b, null)));}if (isImplementsOf(type, Comparable.class)) {ret = ((Comparable) m.invoke(a, null)).compareTo((Comparable) m.invoke(b, null));} else {ret = String.valueOf(m.invoke(a, null)).compareTo(String.valueOf(m.invoke(b, null)));}} catch (NoSuchMethodException ne) {System.out.println(ne);} catch (IllegalAccessException ie) {System.out.println(ie);} catch (InvocationTargetException it) {System.out.println(it);}if (sort != null && sort.toLowerCase().equals(DESC)) {return -ret;} else {return ret;}}});return list;}@SuppressWarnings("unchecked")public static List<?> sortByMethod(List<?> list, final String methods[],final String sorts[]) {if (methods != null && methods.length > 0) {for (int i = methods.length - 1; i >= 0; i--) {final String method = methods[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() {public int compare(Object a, Object b) {int ret = 0;try {Method m = a.getClass().getMethod(method, null);m.setAccessible(true);Class<?> type = m.getReturnType();if (type == int.class) {ret = ((Integer) m.invoke(a, null)).compareTo((Integer) m.invoke(b, null));} else if (type == double.class) {ret = ((Double) m.invoke(a, null)).compareTo((Double) m.invoke(b, null));} else if (type == long.class) {ret = ((Long) m.invoke(a, null)).compareTo((Long) m.invoke(b, null));} else if (type == float.class) {ret = ((Float) m.invoke(a, null)).compareTo((Float) m.invoke(b, null));} else if (type == Date.class) {ret = ((Date) m.invoke(a, null)).compareTo((Date) m.invoke(b, null));} else if (isImplementsOf(type, Comparable.class)) {ret = ((Comparable) m.invoke(a, null)).compareTo((Comparable) m.invoke(b,null));} else {ret = String.valueOf(m.invoke(a, null)).compareTo(String.valueOf(m.invoke(b, null)));}} catch (NoSuchMethodException ne) {System.out.println(ne);} catch (IllegalAccessException ie) {System.out.println(ie);} catch (InvocationTargetException it) {System.out.println(it);}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;}}
1 0
- List集合按照对象中某个属性排序通用工具类(按对象中某属性排序)
- List集合对象中按照不同属性大小排序
- 按照list中对象的属性排序
- list中对象按照属性排序
- sort方法对象中按照某个属性排序
- list集合中按照指定属性排序
- C#List<>能按照List中的对象某个属性排序
- C#中List实现按照对象属性排序
- 关于泛型list中按照对象任意属性排序
- List中对象按照属性时间升序排序
- 数组排序(按对象中某个属性进行排序)
- Java集合按照对象的某个属性进行比较排序
- Java中对象按照属性排序
- 【Java中对象按照属性排序】
- List对象利用属性进行排序的通用工具类
- 对list集合中的对象按照字段属性排序
- List 中对象按属性排序
- java工具类之按对象中某属性排序
- sizeof运算符的内存对齐问题
- Android文本输入框EditText属性和方法说明
- Android ProgressBar自定义图片进度,自定义渐变色进度条
- HDU5407 CRB and Candies
- Django连接SQL Server配置指引
- List集合按照对象中某个属性排序通用工具类(按对象中某属性排序)
- mmap函数用法及示例程序
- 虚拟机与主机不能ping通的解决方法
- 关于for循环遇到的一些错误
- 2015.08.29,今天开通CSDN博客了!
- Joison博客路之开启篇
- Oracle(四)--> 高级子查询(多例详解难度高,实在不信进来看~)
- Socket编程(1)
- RPG黑暗之光(6)状态栏/技能系统/武器系统/小地图/头像栏