java中List按照指定字段排序工具類
文章標題:java中List按照指定字段排序工具類.
文章地址: https://blog.csdn.net/5iasp/article/details/17717179
包括如下幾個類
1. 實體類
package com.newyear.wish; /** * 實體類 * */ public class Video { public Video(int id, String title, int hits) { super(); this.id = id; this.title = title; this.hits = hits; } private int id; private String title; private int hits; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getHits() { return hits; } public void setHits(int hits) { this.hits = hits; } }
2. list排序工具類
package com.newyear.wish; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; /** * List按照指定字段排序工具類 * * @param <T> */ public class ListSortUtil<T> { /** * @param targetList 目標排序List * @param sortField 排序字段(實體類屬性名) * @param sortMode 排序方式(asc or desc) */ @SuppressWarnings({ "unchecked", "rawtypes" }) public void sort(List<T> targetList, final String sortField, final String sortMode) { Collections.sort(targetList, new Comparator() { public int compare(Object obj1, Object obj2) { int retVal = 0; try { //首字母轉大寫 String newStr=sortField.substring(0, 1).toUpperCase()+sortField.replaceFirst("\\w",""); String methodStr="get"+newStr; Method method1 = ((T)obj1).getClass().getMethod(methodStr, null); Method method2 = ((T)obj2).getClass().getMethod(methodStr, null); if (sortMode != null && "desc".equals(sortMode)) { retVal = method2.invoke(((T) obj2), null).toString().compareTo(method1.invoke(((T) obj1), null).toString()); // 倒序 } else { retVal = method1.invoke(((T) obj1), null).toString().compareTo(method2.invoke(((T) obj2), null).toString()); // 正序 } } catch (Exception e) { throw new RuntimeException(); } return retVal; } }); } }
3. 測試類
package com.newyear.wish; import java.util.ArrayList; import java.util.List; public class ListSortUtilTest { /** * @param args */ public static void main(String[] args) { List<Video> targetList = new ArrayList<Video>(); targetList.add(new Video(1,"title1",31)); targetList.add(new Video(2,"title2",12)); targetList.add(new Video(3,"title3",53)); System.out.println("排序前: " + targetList); ListSortUtil<Video> sortList = new ListSortUtil<Video>(); sortList.sort(targetList, "hits", "asc"); System.out.println("排序後:" +targetList); } }
測試執行結果:
排序前: [com.newyear.wish.Video@c17164,
com.newyear.wish.Video@1fb8ee3,
com.newyear.wish.Video@61de33]
排序後:[com.newyear.wish.Video@1fb8ee3,
com.newyear.wish.Video@c17164,
com.newyear.wish.Video@61de33]
最後更新:2017-04-03 12:54:02
上一篇:
使用Style自定義ListView快速滑動圖標
下一篇:
HI3531編譯helloworld,執行錯誤
JQuery this 和 $(this) 詳解
How to get the MouseEvent coordinates for an element that has CSS3 Transform?
曾鳴:互聯網的本質是什麼?| 內部幹貨
關於c# 枚舉 enum 的學習
WinForm中提示Circular base class dependency involving 'TestEncryption.Form' and 'TestEncryption.Form
《LINUX設備驅動程序》學習劄記(一)
A+B Problem III
Hadoop全分布式環境搭建
2013年IT界25個最古怪的麵試題
E-MapReduce集群中HDFS服務集成Kerberos