首页 > 代码库 > List自定义排序(可多条件)

List自定义排序(可多条件)

1:先建需要排序的属性Model

 1 package com.msqsoft.app.queuing.util;
 2 
 3 public class ListOrderByModel {
 4     
 5     private String queueNumber;
 6 
 7     private Integer status;
 8 
 9     public String getQueueNumber() {
10         return queueNumber;
11     }
12 
13     public void setQueueNumber(String queueNumber) {
14         this.queueNumber = queueNumber;
15     }
16 
17     public Integer getStatus() {
18         return status;
19     }
20 
21     public void setStatus(Integer status) {
22         this.status = status;
23     }
24     
25     public ListOrderByModel(String queueNumber, Integer status){
26         this.queueNumber = queueNumber;
27         this.status = status;
28     }
29 }

2:建立排序 Util 定义排序方式 需实现 Comparator  

 1 package com.msqsoft.app.queuing.util;
 2 
 3 import java.util.Comparator;
 4 
 5 public class ListOrderByComparatorModelUtil implements Comparator {
 6 
 7     @Override
 8     public int compare(Object obj1, Object obj2) {
 9         
10         ListOrderByModel orderByModel1 = (ListOrderByModel)obj1;
11         ListOrderByModel orderByModel2 = (ListOrderByModel)obj2;
12         
13         // 先排序status
14         if (orderByModel1.getStatus() > orderByModel2.getStatus()) {
15             return -1;
16         } else if (orderByModel1.getStatus() < orderByModel2.getStatus()) {
17             return 1;
18         } else {
19             // 排序号码
20             return orderByModel1.getQueueNumber().compareTo(orderByModel2.getQueueNumber());
21         }
22     }
23 
24 }

3 : 测试

 1 package web;
 2 
 3 import java.text.Collator;
 4 import java.text.ParseException;
 5 import java.text.SimpleDateFormat;
 6 import java.util.ArrayList;
 7 import java.util.Collections;
 8 import java.util.Comparator;
 9 import java.util.Date;
10 import java.util.List;
11 
12 import com.msqsoft.app.queuing.util.ListOrderByComparatorModelUtil;
13 import com.msqsoft.app.queuing.util.ListOrderByModel;
14 
15 public class Test1 {
16     @SuppressWarnings("unchecked")
17     public static void main(String[] args) throws ParseException {
18         
19         List list = new ArrayList();
20         list.add(new ListOrderByModel("A002", 0));
21         list.add(new ListOrderByModel("A001", 0));
22         list.add(new ListOrderByModel("A003", 0));
23         list.add(new ListOrderByModel("A006", 6));
24         list.add(new ListOrderByModel("A004", 0));
25         list.add(new ListOrderByModel("A008", 5));
26         list.add(new ListOrderByModel("A005", 0));
27         list.add(new ListOrderByModel("A007", 6));
28 
29         ListOrderByComparatorModelUtil comparator = new ListOrderByComparatorModelUtil();
30         Collections.sort(list, comparator);
31         for (int i = 0; i < list.size(); i++) {
32             ListOrderByModel listMode = (ListOrderByModel) list.get(i);
33             System.out.println(listMode.getStatus() + "," + listMode.getQueueNumber());
34         }
35     }
36 }

 

List自定义排序(可多条件)