java list按照元素物件的指定多個欄位屬性進行排序
阿新 • • 發佈:2018-11-12
ListUtils.Java---功能類
http://blog.csdn.net/jiangyu1013/article/details/53894218
[java] view plain copy
- package com.enable.common.utils;
- import java.lang.reflect.Field;
- import java.text.NumberFormat;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.Date;
- import java.util.List;
- /**
- * @author yinaibang
- * 在資料庫中查出來的列表中,往往需要對不同的欄位重新排序。 一般的做法都是使用排序的欄位,重新到資料庫中查詢。
- * 如果不到資料庫查詢,直接在第一次查出來的list中排序,無疑會提高系統的效能。 下面就寫一個通用的方法,對list排序,
- *
- * 至少需要滿足以下5點:
- *
- * ①.list元素物件型別任意
- * ---->使用泛型解決
- *
- * ②.可以按照list元素物件的任意多個屬性進行排序,即可以同時指定多個屬性進行排序
- * --->使用java的可變引數解決
- *
- * ③.list元素物件屬性的型別可以是數字(byte、short、int、long、float、double等,包括正數、負數、0)、字串(char、String)、日期(java.util.Date)
- * --->對於數字:統一轉換為固定長度的字串解決,比如數字3和123,轉換為"003"和"123" ;再比如"-15"和"7"轉換為"-015"和"007"
- * --->對於日期:可以先把日期轉化為long型別的數字,數字的解決方法如上
- *
- * ④.list元素物件的屬性可以沒有相應的getter和setter方法
- * --->可以使用java反射進行獲取private和protected修飾的屬性值
- *
- * ⑤.list元素物件的物件的每個屬性都可以指定是升序還是降序
- * -->使用2個重寫的方法(一個方法滿足所有屬性都按照升序(降序),另外一個方法滿足每個屬性都能指定是升序(降序))
- *
- *
- */
- public class ListUtils {
- /**
- * 對list的元素按照多個屬性名稱排序,
- * list元素的屬性可以是數字(byte、short、int、long、float、double等,支援正數、負數、0)、char、String、java.util.Date
- *
- *
- * @param lsit
- * @param sortname
- * list元素的屬性名稱
- * @param isAsc
- * true升序,false降序
- */
- public static <E> void sort(List<E> list, final boolean isAsc, final String... sortnameArr) {
- Collections.sort(list, new Comparator<E>() {
- public int compare(E a, E b) {
- int ret = 0;
- try {
- for (int i = 0; i < sortnameArr.length; i++) {
- ret = ListUtils.compareObject(sortnameArr[i], isAsc, a, b);
- if (0 != ret) {
- break;
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return ret;
- }
- });
- }
- /**
- * 給list的每個屬性都指定是升序還是降序
- *
- * @param list
- * @param sortnameArr 引數陣列
- * @param typeArr 每個屬性對應的升降序陣列, true升序,false降序
- */
- public static <E> void sort(List<E> list, final String[] sortnameArr, final boolean[] typeArr) {
- if (sortnameArr.length != typeArr.length) {
- throw new RuntimeException("屬性陣列元素個數和升降序陣列元素個數不相等");
- }
- Collections.sort(list, new Comparator<E>() {
- public int compare(E a, E b) {
- int ret = 0;
- try {
- for (int i = 0; i < sortnameArr.length; i++) {
- ret = ListUtils.compareObject(sortnameArr[i], typeArr[i], a, b);
- if (0 != ret) {
- break;
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return ret;
- }
- });
- }
- /**
- * 對2個物件按照指定屬性名稱進行排序
- *
- * @param sortname
- * 屬性名稱
- * @param isAsc
- * true升序,false降序
- * @param a
- * @param b
- * @return
- * @throws Exception
- */
- private static <E> int compareObject(final String sortname, final boolean isAsc, E a, E b) throws Exception {
- int ret;
- Object value1 = ListUtils.forceGetFieldValue(a, sortname);
- Object value2 = ListUtils.forceGetFieldValue(b, sortname);
- String str1 = value1.toString();
- String str2 = value2.toString();
- if (value1 instanceof Number && value2 instanceof Number) {
- int maxlen = Math.max(str1.length(), str2.length());
- str1 = ListUtils.addZero2Str((Number) value1, maxlen);
- str2 = ListUtils.addZero2Str((Number) value2, maxlen);
- } else if (value1 instanceof Date && value2 instanceof Date) {
- long time1 = ((Date) value1).getTime();
- long time2 = ((Date) value2).getTime();
- int maxlen = Long.toString(Math.max(time1, time2)).length();
- str1 = ListUtils.addZero2Str(time1, maxlen);
- str2 = ListUtils.addZero2Str(time2, maxlen);
- }
- if (isAsc) {
- ret = str1.compareTo(str2);
- } else {
- ret = str2.compareTo(str1);
- }
- return ret;
- }
- /**
- * 給數字物件按照指定長度在左側補0.
- *
- * 使用案例: addZero2Str(11,4) 返回 "0011", addZero2Str(-18,6)返回 "-000018"
- *
- * @param numObj
- * 數字物件
- * @param length
- * 指定的長度
- * @return
- */
- public static String addZero2Str(Number numObj, int length) {
- NumberFormat nf = NumberFormat.getInstance();
- // 設定是否使用分組
- nf.setGroupingUsed(false);
- // 設定最大整數位數
- nf.setMaximumIntegerDigits(length);
- // 設定最小整數位數
- nf.setMinimumIntegerDigits(length);
- return nf.format(numObj);
- }
- /**
- * 獲取指定物件的指定屬性值(去除private,protected的限制)
- *
- * @param obj
- * 屬性名稱所在的物件
- * @param fieldName
- * 屬性名稱
- * @return
- * @throws Exception
- */
- public static Object forceGetFieldValue(Object obj, String fieldName) throws Exception {
- Field field = obj.getClass().getDeclaredField(fieldName);
- Object object = null;
- boolean accessible = field.isAccessible();
- if (!accessible) {
- // 如果是private,protected修飾的屬性,需要修改為可以訪問的
- field.setAccessible(true);
- object = field.get(obj);
- // 還原private,protected屬性的訪問性質
- field.setAccessible(accessible);
- return object;
- }
- object = field.get(obj);
- return object;
- }
- }
UserInfo.java
[java] view plain copy
- package com;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- /**
- *
- * @author yinaibang
- *
- */
- public class UserInfo implements java.io.Serializable {
- private static final long serialVersionUID = -3522051445403971732L;
- private Integer userId;
- private String username;
- private Date birthDate;
- private Integer age;
- private float fRate;
- private char ch;
- public Date getBirthDate() {
- return birthDate;
- }
- public String getBirthDatestr() {
- SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
- return formater.format(getBirthDate());
- }
- public UserInfo(Integer userId, String username, Date birthDate, Integer age, float fRate, char ch) {
- super();
- this.userId = userId;
- this.username = username;
- this.birthDate = birthDate;
- this.age = age;
- this.fRate = fRate;
- this.ch = ch;
- }
- @Override
- public String toString() {
- return "UserInfo [userId=" + userId + ", \tusername=" + username + ", \tbirthDate=" + getBirthDatestr()
- + ", \tage=" + age + ", fRate=" + fRate + ", ch=" + ch + "]";
- }
- }
Test.java---測試類
[java] view plain copy
- package com;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.List;
- import com.enable.common.utils.ListUtils;
- /**
- *
- * @author yinaibang
- *
- */
- public class Test {
- public static void main(String[] args) throws Exception {
- Test testObj = new Test();
- List<UserInfo> list = new ArrayList<UserInfo>();
- // public UserInfo(Integer userId, String username, Date birthDate,Integer age, float fRate, char ch)
- SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
- UserInfo user1 = new UserInfo(3, "bbb", formater.parse("1980-12-01"), 1, 1.2f, 'a');
- UserInfo user2 = new UserInfo(0, "126", formater.parse("1900-10-11"), 03, -3.6f, 'c');
- UserInfo user3 = new UserInfo(12, "5", formater.parse("1973-08-21"), 15, 9.32f, 'f');
- UserInfo user4 = new UserInfo(465, "1567", formater.parse("2012-01-26"), 20, 12.56f, '0');
- UserInfo user5 = new UserInfo(2006, "&4m", formater.parse("2010-05-08"), 100, 165.32f, '5');
- UserInfo user6 = new UserInfo(5487, "hf67", formater.parse("2016-12-30"), 103, 56.32f, 'm');
- UserInfo user7 = new UserInfo(5487,"jigg", formater.parse("2000-10-16"), 103, 56.32f, 'm');
- UserInfo user8 = new UserInfo(5487, "jigg", formater.parse("1987-07-25"), 103, 56.32f, 'm');
- list.add(user1);
- list.add(user2);
- list.add(user3);
- list.add(user4);
- list.add(user5);
- list.add(user6);
- list.add(user7);
- list.add(user8);
- System.out.println("\n-------原來序列-------------------");
- testObj.printfUserInfoList(list);
- // 按userId升序、username降序、birthDate升序排序
- String [] sortNameArr = {"userId","username","birthDate"};
- boolean [] isAscArr = {true,false,true};
- ListUtils.sort(list,sortNameArr,isAscArr);
- System.out.println("\n--------按按userId升序、username降序、birthDate升序排序(如果userId相同,則按照username降序,如果username相同,則按照birthDate升序)------------------");
- testObj.printfUserInfoList(list);
- // 按userId、username、birthDate都升序排序
- ListUtils.sort(list, true, "userId", "username","birthDate");
- System.out.println("\n--------按userId、username、birthDate排序(如果userId相同,則按照username升序,如果username相同,則按照birthDate升序)------------------");
- testObj.printfUserInfoList(list);
- // 按userId、username都倒序排序
- ListUtils.sort(list, false, "userId", "username");
- System.out.println("\n--------按userId和username倒序(如果userId相同,則按照username倒序)------------------");
- testObj.printfUserInfoList(list);
- // 按username、birthDate都升序排序
- ListUtils.sort(list, true, "username", "birthDate");
- System.out.println("\n---------按username、birthDate升序(如果username相同,則按照birthDate升序)-----------------");
- testObj.printfUserInfoList(list);
- // 按birthDate倒序排序
- ListUtils.sort(list, false, "birthDate");
- System.out.println("\n---------按birthDate倒序-----------------");
- testObj.printfUserInfoList(list);
- // 按fRate升序排序
- ListUtils.sort(list, true, "fRate");
- System.out.println("\n---------按fRate升序-----------------");
- testObj.printfUserInfoList(list);
- // 按ch倒序排序
- ListUtils.sort(list, false, "ch");
- System.out.println("\n---------按ch倒序-----------------");
- testObj.printfUserInfoList(list);
- }
- private void printfUserInfoList(List<UserInfo> list) {
- for (UserInfo user : list) {
- System.out.println(user.toString());
- }
- }
- }
執行結果
[java] view plain copy
- -------原來序列-------------------
- UserInfo [userId=3, username=bbb, birthDate=1980-12-01, age=1, fRate=1.2, ch=a]
- UserInfo [userId=0, username=126, birthDate=1900-10-11, age=3, fRate=-3.6, ch=c]
- UserInfo [userId=12, username=5, birthDate=1973-08-21, age=15, fRate=9.32, ch=f]
- UserInfo [userId=465, username=1567, birthDate=2012-01-26, age=20, fRate=12.56, ch=0]
- UserInfo [userId=2006, username=&4m, birthDate=2010-05-08, age=100, fRate=165.32, ch=5]
- UserInfo [userId=5487, username=hf67, birthDate=2016-12-30, age=103, fRate=56.32, ch=m]
- UserInfo [userId=5487, username=jigg, birthDate=2000-10-16, age=103, fRate=56.32, ch=m]
- UserInfo [userId=5487, username=jigg, birthDate=1987-07-25, age=103, fRate=56.32, ch=m]
- --------按按userId升序、username降序、birthDate升序排序(如果userId相同,則按照username降序,如果username相同,則按照birthDate升序)------------------
- UserInfo [userId=0, username=126, birthDate=1900-10-11, age=3, fRate=-3.6, ch=c]
- UserInfo [userId=3, username=bbb, birthDate=1980-12-01, age=1, fRate=1.2, ch=a]
- UserInfo [userId=12, username=5, birthDate=1973-08-21, age=15, fRate=9.32, ch=f]
- UserInfo [userId=465, username=1567, birthDate=2012-01-26, age=20, fRate=12.56, ch=0]
- UserInfo [userId=2006, username=&4m, birthDate=2010-05-08, age=100, fRate=165.32, ch=5]
- UserInfo [userId=5487, username=jigg, birthDate=1987-07-25, age=103, fRate=56.32, ch=m]
- UserInfo [userId=5487, username=jigg, birthDate=2000-10-16, age=103, fRate=56.32, ch=m]
- UserInfo [userId=5487, username=hf67, birthDate=2016-12-30, age=103, fRate=56.32, ch=m]
- --------按userId、username、birthDate排序(如果userId相同,則按照username升序,如果username相同,則按照birthDate升序)------------------
- UserInfo [userId=0, username=126, birthDate=1900-10-11, age=3, fRate=-3.6, ch=c]
- UserInfo [userId=3, username=bbb, birthDate=1980-12-01, age=1, fRate=1.2, ch=a]
- UserInfo [userId=12, username=5, birthDate=1973-08-21, age=15, fRate=9.32, ch=f]
- UserInfo [userId=465, username=1567, birthDate=2012-01-26, age=20, fRate=12.56, ch=0]
- UserInfo [userId=2006, username=&4m, birthDate=2010-05-08, age=100, fRate=165.32, ch=5]
- UserInfo [userId=5487, username=hf67, birthDate=2016-12-30, age=103, fRate=56.32, ch=m]
- UserInfo [userId=5487, username=jigg, birthDate=1987-07-25, age=103, fRate=56.32, ch=m]
- UserInfo [userId=5487, username=jigg, birthDate=2000-10-16, age=103, fRate=56.32, ch=m]
- --------按userId和username倒序(如果userId相同,則按照username倒序)------------------
- UserInfo [userId=5487, username=jigg, birthDate=1987-07-25, age=103, fRate=56.32, ch=m]
- UserInfo [userId=5487, username=jigg, birthDate=2000-10-16, age=103, fRate=56.32, ch=m]
- UserInfo [userId=5487, username=hf67, birthDate=2016-12-30, age=103, fRate=56.32, ch=m]
- UserInfo [userId=2006, username=&4m, birthDate=2010-05-08, age=100, fRate=165.32, ch=5]
- UserInfo [userId=465, username=1567, birthDate=2012-01-26, age=20, fRate=12.56, ch=0]
- UserInfo [userId=12, username=5, birthDate=1973-08-21, age=15, fRate=9.32, ch=f]
- UserInfo [userId=3, username=bbb, birthDate=1980-12-01, age=1, fRate=1.2, ch=a]
- UserInfo [userId=0, username=126, birthDate=1900-10-11, age=3, fRate=-3.6, ch=c]
- ---------按username、birthDate升序(如果username相同,則按照birthDate升序)-----------------
- UserInfo [userId=2006, username=&4m, birthDate=2010-05-08, age=100, fRate=165.32, ch=5]
- UserInfo [userId=0, username=126, birthDate=1900-10-11, age=3, fRate=-3.6, ch=c]
- UserInfo [userId=465, username=1567, birthDate=2012-01-26, age=20, fRate=12.56, ch=0]
- UserInfo [userId=12, username=5, birthDate=1973-08-21, age=15, fRate=9.32, ch=f]
- UserInfo [userId=3, username=bbb, birthDate=1980-12-01, age=1, fRate=1.2, ch=a]
- UserInfo [userId=5487, username=hf67, birthDate=2016-12-30, age=103, fRate=56.32, ch=m]
- UserInfo [userId=5487, username=jigg, birthDate=1987-07-25, age=103, fRate=56.32, ch=m]
- UserInfo [userId=5487, username=jigg, birthDate=2000-10-16, age=103, fRate=56.32, ch=m]
- ---------按birthDate倒序-----------------
- UserInfo [userId=5487, username=hf67, birthDate=2016-12-30, age=103, fRate=56.32, ch=m]
- UserInfo [userId=465, username=1567, birthDate=2012-01-26, age=20, fRate=12.56, ch=0]
- UserInfo [userId=2006, username=&4m, birthDate=2010-05-08, age=100, fRate=165.32, ch=5]
- UserInfo [userId=5487, username=jigg, birthDate=2000-10-16, age=103, fRate=56.32, ch=m]
- UserInfo [userId=5487, username=jigg, birthDate=1987-07-25, age=103, fRate=56.32, ch=m]
- UserInfo [userId=3, username=bbb, birthDate=1980-12-01, age=1, fRate=1.2, ch=a]
- UserInfo [userId=12, username=5, birthDate=1973-08-21, age=15, fRate=9.32, ch=f]
- UserInfo [userId=0, username=126, birthDate=1900-10-11, age=3, fRate=-3.6, ch=c]
- ---------按fRate升序-----------------
- UserInfo [userId=0, username=126, birthDate=1900-10-11, age=3, fRate=-3.6, ch=c]
- UserInfo [userId=3, username=bbb, birthDate=1980-12-01, age=1, fRate=1.2, ch=a]
- UserInfo [userId=12, username=5, birthDate=1973-08-21, age=15, fRate=9.32, ch=f]
- UserInfo [userId=465, username=1567, birthDate=2012-01-26, age=20, fRate=12.56, ch=0]
- UserInfo [userId=5487, username=hf67, birthDate=2016-12-30, age=103, fRate=56.32, ch=m]
- UserInfo [userId=5487, username=jigg, birthDate=2000-10-16, age=103, fRate=56.32, ch=m]
- UserInfo [userId=5487, username=jigg, birthDate=1987-07-25, age=103, fRate=56.32, ch=m]
- UserInfo [userId=2006, username=&4m, birthDate=2010-05-08, age=100, fRate=165.32, ch=5]
- ---------按ch倒序-----------------
- UserInfo [userId=5487, username=hf67, birthDate=2016-12-30, age=103, fRate=56.32, ch=m]
- UserInfo [userId=5487, username=jigg, birthDate=2000-10-16, age=103, fRate=56.32, ch=m]
- UserInfo [userId=5487, username=jigg, birthDate=1987-07-25, age=103, fRate=56.32, ch=m]
- UserInfo [userId=12, username=5, birthDate=1973-08-21, age=15, fRate=9.32, ch=f]
- UserInfo [userId=0, username=126, birthDate=1900-10-11, age=3, fRate=-3.6, ch=c]
- UserInfo [userId=3, username=bbb, birthDate=1980-12-01, age=1, fRate=1.2, ch=a]
- UserInfo [userId=2006, username=&4m, birthDate=2010-05-08, age=100, fRate=165.32, ch=5]
- UserInfo [userId=465, username=1567, birthDate=2012-01-26, age=20, fRate=12.56, ch=0]