由bean组成的list根据某一字段排序

Posted miss茶茶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了由bean组成的list根据某一字段排序相关的知识,希望对你有一定的参考价值。

  1 package com.chinfotech.common.util;
  2 import java.util.ArrayList;
  3 import java.util.Collections;
  4 import java.util.Comparator;
  5 import java.util.LinkedHashMap;
  6 import java.util.List;
  7 import java.util.Map;
  8 
  9 import org.apache.commons.beanutils.BeanComparator;
 10 import org.apache.commons.collections.ComparatorUtils;
 11 import org.apache.commons.collections.comparators.ComparableComparator;
 12 import org.apache.commons.collections.comparators.ComparatorChain;
 13 import org.apache.commons.collections.comparators.NullComparator;
 14 
 15 import com.chinfotech.scenicManage.bean.SaleTim;
 16 /**
 17  * 
 18  * @author gaomengqin
 19  * 将bean封装成list后按照某一字段排序
 20  *
 21  */
 22 
 23 public class ListSort {
 24 
 25     /**
 26      * bean的某一个属性
 27      * @param list:列表名称
 28      * @param filedName:属性名称
 29      * @param ascFlag:true;升序;false:降序
 30      */
 31     @SuppressWarnings("unchecked")
 32     public static void sort(List list, String filedName, boolean ascFlag) {
 33 
 34         if (list.size() == 0 || filedName.equals("")) {
 35             return;
 36         }
 37         Comparator<?> cmp = ComparableComparator.getInstance();
 38         // 判断升序还是降序
 39         if (ascFlag) {
 40             cmp = ComparatorUtils.nullLowComparator(cmp);
 41         } else {
 42             cmp = ComparatorUtils.reversedComparator(cmp);
 43 
 44         }
 45         Collections.sort(list, new BeanComparator(filedName, cmp));
 46     }
 47     
 48     /**
 49      * bean根据某几个属性排序
 50      * @param beans
 51      * @param sortParam
 52      */
 53     @SuppressWarnings({ "rawtypes", "unchecked" })
 54     public static void sortExecute(List<?> beans, Map<String, String> sortParam) {
 55 
 56         // 判断beans和排序规则是否有内容
 57         if (beans.size() == 0 || sortParam.keySet().size() == 0) {
 58             return;
 59         }
 60 
 61         // 判断beans和排序规则是否有内容
 62         if (beans.isEmpty() || sortParam.isEmpty()) {
 63             return;
 64         }
 65         ComparatorChain comparator = new ComparatorChain();
 66         boolean sortMethod = false;
 67         for (String itemName : sortParam.keySet()) {
 68             sortMethod = false;
 69             if ("desc".equals(sortParam.get(itemName))) {
 70                 sortMethod = true;
 71             }
 72             comparator.addComparator(new BeanComparator(itemName, new NullComparator()), sortMethod);
 73         }
 74         Collections.sort(beans, comparator);
 75     }
 76     
 77     public static void main(String[] args)
 78     {
 79         // TODO Auto-generated method stub
 80           
 81         //1、单纯的列表为例
 82         ArrayList lst = new ArrayList();
 83         lst.add("00000000001");
 84         lst.add("00000000002");
 85         lst.add("00000000003");
 86         lst.add("00000000004");
 87         lst.add("00000000005");
 88         //单纯调用升序,无需改写方法
 89         Collections.sort(lst);
 90         System.out.println(lst.toString());
 91         //单纯调用降序,无需改写方法
 92         Collections.reverse(lst);
 93         System.out.println(lst.toString());
 94         //2、List<?> beans 为例
 95         List<SaleTim> list = new ArrayList<SaleTim>();
 96         SaleTim sb;
 97         sb = new SaleTim();
 98         sb.setCheck("1006");
 99         sb.setSale("3456");
100         sb.setTime("18:00");
101         list.add(sb);
102         
103         sb = new SaleTim();
104         sb.setCheck("2378");
105         sb.setSale("1265");
106         sb.setTime("17:00");
107         list.add(sb);
108         
109         sb = new SaleTim();
110         sb.setCheck("1026");
111         sb.setSale("2390");
112         sb.setTime("19:30");
113         list.add(sb);
114         
115         sb = new SaleTim();
116         sb.setCheck("2900");
117         sb.setSale("1089");
118         sb.setTime("15:30");
119         list.add(sb);
120         
121         
122         sb = new SaleTim();
123         sb.setCheck("2000");
124         sb.setSale("3678");
125         sb.setTime("16:30");
126         list.add(sb);
127         
128         sb = new SaleTim();
129         sb.setCheck("4500");
130         sb.setSale("4000");
131         sb.setTime("17:30");
132         list.add(sb);
133         System.out.println("***********原序*************");
134         //原序
135         for(SaleTim item : list){
136             System.out.print(item.getCheck()+"    ");
137             System.out.print(item.getSale()+"    ");
138             System.out.println(item.getTime());
139         }
140         
141         //降順
142         System.out.println("***********check:降序*************");
143         ListSort.sort(list, "check", false);
144         for(SaleTim item : list){
145             System.out.print(item.getCheck()+"    ");
146             System.out.print(item.getSale()+"    ");
147             System.out.println(item.getTime());
148         }
149         //昇順
150         System.out.println("***********check:升序*************");
151         ListSort.sort(list, "check", true);
152         for(SaleTim item : list){
153               System.out.print(item.getCheck()+"    ");
154               System.out.print(item.getSale()+"    ");
155               System.out.println(item.getTime());
156         }
157         //昇順
158         System.out.println("***********time:升序*************");
159         ListSort.sort(list, "time", true);
160         for(SaleTim item : list){
161               System.out.print(item.getCheck()+"    ");
162               System.out.print(item.getSale()+"    ");
163               System.out.println(item.getTime());
164         }
165         //降順
166         System.out.println("***********time:降序*************");
167         ListSort.sort(list, "time", false);
168         for(SaleTim item : list){
169             System.out.print(item.getCheck()+"    ");
170             System.out.print(item.getSale()+"    ");
171             System.out.println(item.getTime());
172         }
173         System.out.println("***********check:升序 time:降序*************");
174         //3、LIST<?>beans 多个属性叠加排序
175         Map<String, String> sortParam = new LinkedHashMap<String, String>();
176         //check:昇順 time:降順
177         sortParam.put("check", "asc");
178         sortParam.put("time", "desc");
179         ListSort.sortExecute(list, sortParam);
180         
181         for(SaleTim item : list){
182               System.out.print(item.getCheck()+"    ");
183               System.out.print(item.getSale()+"    ");
184               System.out.println(item.getTime());
185         }
186         sortParam.clear();
187         //check:昇順 time:昇順
188         sortParam.put("check", "desc");
189         sortParam.put("time", "asc");
190         ListSort.sortExecute(list, sortParam);
191         System.out.println("***********check:降順 time:昇順*************");
192         for(SaleTim item : list){
193             System.out.print(item.getCheck()+"    ");
194             System.out.print(item.getSale()+"    ");
195             System.out.println(item.getTime());
196       }
197     }
198 }

 

以上是关于由bean组成的list根据某一字段排序的主要内容,如果未能解决你的问题,请参考以下文章

java 如何判断list里某一字段值全部相等

JDK1.8对List指定字段进行排序

Java中List对象集合按照对象某一属性字段排序

算法-快速排序

java List<T>排序

java8中List根据某一字段去重