All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.base4j.util.SortUtil Maven / Gradle / Ivy

The newest version!
package com.base4j.util;

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;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 排序工具类
* * @author huangjun * @version [版本号, 2017年11月17日] * @Description: * @ClassName:ListSortUtil * @see [相关类/方法] * @since [产品/模块] */ public class SortUtil { public static final String ASC = "asc"; public static final String DESC = "desc"; private static Pattern NUMBER_PATTERN = Pattern.compile("^[+-]?[0-9]+$"); /** * [简述]: 对List数组排序 * * @param list 源数据 排序集合 * @param sort 升序 还是 降序,默认升序 * @return List */ public static List sort(List list, final String sort) { Collections.sort(list, new Comparator() { @Override public int compare(Object o1, Object o2) { int ret = 0; if (o1 instanceof Integer) { ret = ((Integer) o1).compareTo((Integer) o2); } else if (o1 instanceof Double) { ret = ((Double) o1).compareTo((Double) o2); } else if (o1 instanceof Long) { ret = ((Long) o1).compareTo((Long) o2); } else if (o1 instanceof Float) { ret = ((Float) o1).compareTo((Float) o2); } else if (o1 instanceof Date) { ret = ((Date) o1).compareTo((Date) o2); } else if (isDouble(String.valueOf(o1)) && isDouble(String.valueOf(o2))) { ret = (new Double(o1.toString())).compareTo(new Double(o2.toString())); } else { ret = String.valueOf(o1).compareTo(String.valueOf(o2)); } if (null != sort && DESC.equalsIgnoreCase(sort)) { return -ret; } else { return ret; } } }); return list; } /** * [简述]: List 泛型 排序 * * @param list 源数据 排序集合 * @param field 排序的数据字段名称 * @param sort 升序 还是 降序,默认升序 * @param 泛型T * @return List */ public static List sort(List list, final String field, final String sort) { SortListByCompartor(list, field, sort); return list; } private static void SortListByCompartor(List list, final String field, final String sort) { Collections.sort(list, new Comparator() { @Override public int compare(T o1, T o2) { int ret = 0; try { Method method1 = o1.getClass().getDeclaredMethod(getMethodName(field)); Method method2 = o2.getClass().getDeclaredMethod(getMethodName(field)); Field field1 = o1.getClass().getDeclaredField(field); Field field2 = o2.getClass().getDeclaredField(field); field1.setAccessible(true); field2.setAccessible(true); Class type = field1.getType(); if (type == int.class) { ret = ((Integer) field1.getInt(o1)).compareTo((Integer) field2.getInt(o2)); } else if (type == double.class) { ret = ((Double) field1.getDouble(o1)).compareTo((Double) field2.getDouble(o2)); } else if (type == long.class) { ret = ((Long) field1.getLong(o1)).compareTo((Long) field2.getLong(o2)); } else if (type == float.class) { ret = ((Float) field1.getFloat(o1)).compareTo((Float) field2.getFloat(o2)); } else if (type == Date.class) { ret = ((Date) field1.get(o1)).compareTo((Date) field2.get(o2)); } else if (isDouble(String.valueOf(field1.get(o1))) && isDouble(String.valueOf(field2.get(o2)))) { ret = (new Double(method1.invoke(o1).toString())).compareTo(new Double(method2.invoke(o2) .toString())); } else { Object obj1= ReflectionUtils.getFieldValue(o1,field); Object obj2= ReflectionUtils.getFieldValue(o2,field); ret = String.valueOf(obj1).compareTo(String.valueOf(obj2)); } } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } if (null != sort && DESC.equalsIgnoreCase(sort)) { return -ret; } else { return ret; } } }); } private static boolean isDouble(String str) { boolean flag = false; if (isInteger(str) || isFloat(str)) { flag = true; } return flag; } private static boolean isInteger(String str) { Matcher matcher = NUMBER_PATTERN.matcher(str); return matcher.find(); } private static boolean isFloat(String str) { return str.matches("[\\d]+\\.[\\d]+"); } /** * [简述]: List 泛型 排序 * * @param list 源数据 排序集合 * @param fields 排序的数据字段名称 * @param sorts 升序 还是 降序 * @param 泛型T * @return List */ public static List sort(List list, final String[] fields, final String[] sorts) { if (null != fields && fields.length > 0) { for (int index = 0; index < fields.length; index++) { String sortRule = ASC; if (null != sorts && sorts.length >= index && null != sorts[index]) { sortRule = sorts[index]; } final String sort = sortRule; final String field = fields[index]; SortListByCompartor(list, field, sort); } } return list; } private static String getMethodName(String str) { StringBuffer name = new StringBuffer(); name = name.append("get").append(firstLetterToCapture(str)); return name.toString(); } private static String firstLetterToCapture(String name) { char[] arr = name.toCharArray(); arr[0] -= 32; return String.valueOf(arr); } public static void main(String[] args) { // List arrays = new ArrayList(); // arrays.add("2"); // arrays.add("13"); // arrays.add("4.5"); // arrays.add("26"); // List afterSortArrays = (List) ListSortUtil.sort(arrays, "asc"); // for (String s : afterSortArrays) { // System.out.println(String.valueOf(s)); // } // List arrays = new ArrayList(); // arrays.add("ac"); // arrays.add("cd"); // arrays.add("ab"); // arrays.add("bf"); // arrays.add("bfd"); // arrays.add("bg"); // arrays.add("ba"); // List afterSortArrays = (List) ListSortUtil.sort(arrays, "asc"); // for (String s : afterSortArrays) { // System.out.println(String.valueOf(s)); // } /*List arrays = new ArrayList(); arrays.add(DateUtil.str2date("2015-01-12")); arrays.add(DateUtil.str2date("2015-11-12")); arrays.add(DateUtil.str2date("2015-12-12")); arrays.add(DateUtil.str2date("2015-02-12")); arrays.add(DateUtil.str2date("2015-06-12")); arrays.add(DateUtil.str2date("2015-10-12")); arrays.add(DateUtil.str2date("2015-10-31")); arrays.add(DateUtil.str2date("2015-10-02")); List afterSortArrays = (List) SortUtil.sort(arrays, "asc"); for(Date s:afterSortArrays){ System.out.println(String.valueOf(DateUtil.date2str(s))); }*/ } }