java中List按照指定字段排序工具类

46 篇文章 0 订阅

文章标题:java中List按照指定字段排序工具类.

文章地址: http://blog.csdn.net/5iasp/article/details/17717179

 

包括如下几个类

 

1. 实体类

 

  1. package com.newyear.wish;  
  2.   
  3. /** 
  4.  * 实体类 
  5.  * 
  6.  */  
  7. public class Video {  
  8.   
  9.     public Video(int id, String title, int hits) {  
  10.         super();  
  11.         this.id = id;  
  12.         this.title = title;  
  13.         this.hits = hits;  
  14.     }  
  15.   
  16.     private int id;  
  17.     private String title;  
  18.     private int hits;  
  19.     public int getId() {  
  20.         return id;  
  21.     }  
  22.     public void setId(int id) {  
  23.         this.id = id;  
  24.     }  
  25.     public String getTitle() {  
  26.         return title;  
  27.     }  
  28.     public void setTitle(String title) {  
  29.         this.title = title;  
  30.     }  
  31.     public int getHits() {  
  32.         return hits;  
  33.     }  
  34.     public void setHits(int hits) {  
  35.         this.hits = hits;  
  36.     }  
  37.   
  38. }  


2. list排序工具类

 

  1. package com.newyear.wish;  
  2.   
  3. import java.lang.reflect.Method;  
  4. import java.util.ArrayList;  
  5. import java.util.Collections;  
  6. import java.util.Comparator;  
  7. import java.util.List;  
  8.   
  9. /** 
  10.  *  List按照指定字段排序工具类 
  11.  * 
  12.  * @param <T> 
  13.  */  
  14.   
  15. public class ListSortUtil<T> {  
  16.     /** 
  17.      * @param targetList 目标排序List 
  18.      * @param sortField 排序字段(实体类属性名) 
  19.      * @param sortMode 排序方式(asc or  desc) 
  20.      */  
  21.     @SuppressWarnings({ "unchecked""rawtypes" })  
  22.     public void sort(List<T> targetList, final String sortField, final String sortMode) {  
  23.       
  24.         Collections.sort(targetList, new Comparator() {  
  25.             public int compare(Object obj1, Object obj2) {   
  26.                 int retVal = 0;  
  27.                 try {  
  28.                     //首字母转大写  
  29.                     String newStr=sortField.substring(01).toUpperCase()+sortField.replaceFirst("\\w","");   
  30.                     String methodStr="get"+newStr;  
  31.                       
  32.                     Method method1 = ((T)obj1).getClass().getMethod(methodStr, null);  
  33.                     Method method2 = ((T)obj2).getClass().getMethod(methodStr, null);  
  34.                     if (sortMode != null && "desc".equals(sortMode)) {  
  35.                         retVal = method2.invoke(((T) obj2), null).toString().compareTo(method1.invoke(((T) obj1), null).toString()); // 倒序  
  36.                     } else {  
  37.                         retVal = method1.invoke(((T) obj1), null).toString().compareTo(method2.invoke(((T) obj2), null).toString()); // 正序  
  38.                     }  
  39.                 } catch (Exception e) {  
  40.                     throw new RuntimeException();  
  41.                 }  
  42.                 return retVal;  
  43.             }  
  44.         });  
  45.     }  
  46.       
  47. }  


3. 测试类

  1. package com.newyear.wish;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. public class ListSortUtilTest {  
  7.   
  8.     /** 
  9.      * @param args 
  10.      */  
  11.     public static void main(String[] args) {  
  12.   
  13.           
  14.         List<Video> targetList = new ArrayList<Video>();  
  15.         targetList.add(new Video(1,"title1",31));  
  16.         targetList.add(new Video(2,"title2",12));  
  17.         targetList.add(new Video(3,"title3",53));  
  18.         System.out.println("排序前: " + targetList);  
  19.           
  20.         ListSortUtil<Video> sortList = new ListSortUtil<Video>();  
  21.         sortList.sort(targetList, "hits""asc");  
  22.         System.out.println("排序后:" +targetList);  
  23.   
  24.     }  
  25.   
  26. }  


 


测试执行结果:

排序前: [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]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值