Administrator
2023-04-21 195945efc5db921a4c9eb8cf9421c172273293f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package com.mandi.common;
 
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
import org.apache.commons.lang3.StringUtils;
 
public class ListSortUtil<T> {
    
     @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)) {  
                            if(method1.invoke(((T) obj2), null)==null||method2.invoke(((T) obj1), null)==null){
                                retVal=1;
                            }else {
                                retVal = method1.invoke(((T) obj2), null).toString().compareTo(method2.invoke(((T) obj1), null).toString()); // 倒序  
                            }
                        } else {  
                            if(method2.invoke(((T) obj2), null)==null||method1.invoke(((T) obj1), null)==null){
                                retVal=-1;
                            }else {
                                retVal = method2.invoke(((T) obj1), null).toString().compareTo(method1.invoke(((T) obj2), null).toString()); // 正序  
                            }
                        }  
                    } catch (Exception e) {  
                        throw new RuntimeException();  
                    }  
                    return retVal;  
                }  
            });  
        } 
     
     
     @SuppressWarnings({ "unchecked", "rawtypes" })  
        public void sortFields(List<T> targetList, final List<String> sortFields) {  
          
            Collections.sort(targetList, new Comparator() {  
                public int compare(Object obj1, Object obj2) {   
                    int retVal = 0;  
                    try {  
                        for (int i=0;i<sortFields.size();i++) {
                            String sortField=sortFields.get(i).split("-")[0];
                            String sortMode=sortFields.get(i).split("-")[1];
                            //首字母转大写  
                            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(method1==null|| method2==null){
                                continue;
                            }
                            Type tp =method1.getGenericReturnType();
                            String tt="";
                            boolean sz=false;
                            if(tp!=null){
                                tt=tp.toString();
                                if(!StringUtils.isEmpty(tt)){
                                    if(tt.contains("int")||tt.contains("double")){
                                        sz=true;
                                    }
                                }
                            }
                            if (sortMode != null && "desc".equals(sortMode)) {  
                                if(method2.invoke(((T) obj2), null)==null){
                                    retVal=0;
                                    continue;
                                }else if(method1.invoke(((T) obj1), null)==null){
                                    retVal=0;
                                    continue;
                                }else {
                                    if(sz){
                                        if(!method1.invoke(((T) obj2), null).toString().equals(method2.invoke(((T) obj1), null).toString())){
                                            if(tt.contains("int")){
                                                if(Integer.parseInt(method1.invoke(((T) obj2), null).toString())>Integer.parseInt(method2.invoke(((T) obj1), null).toString())){
                                                    retVal = 1; // 倒序  
                                                }else if(Integer.parseInt(method1.invoke(((T) obj2), null).toString())<Integer.parseInt(method2.invoke(((T) obj1), null).toString())){
                                                    retVal = -1;
                                                }else{
                                                    retVal = 0; // 倒序  
                                                    continue;
                                                }
                                                break;
                                            }else if(tt.contains("double")){
                                                if(Double.parseDouble((method1.invoke(((T) obj2), null).toString()))>Double.parseDouble(method2.invoke(((T) obj1), null).toString())){
                                                    retVal = 1; // 倒序  
                                                }else if(Double.parseDouble(method1.invoke(((T) obj2), null).toString())<Double.parseDouble(method2.invoke(((T) obj1), null).toString())){
                                                    retVal = -1;
                                                }else{
                                                    retVal = 0; // 倒序  
                                                    continue;
                                                }
                                                break;
                                            }
                                        }else{
                                            retVal =0;
                                            continue;
                                        }
                                    }else{
                                        if(!method1.invoke(((T) obj2), null).toString().equals(method2.invoke(((T) obj1), null).toString())){
                                            retVal = method1.invoke(((T) obj2), null).toString().compareTo(method2.invoke(((T) obj1), null).toString()); // 倒序  
                                            break;
                                        }else{
                                            retVal =0;
                                            continue;
                                        }
                                    }
                                    
                                }
                            } else {  
                                if(method1.invoke(((T) obj1), null)==null){
                                    retVal=0;
                                    continue;
                                }else if(method2.invoke(((T) obj2), null)==null){
                                    retVal=0;
                                    continue;
                                }else {
                                    if(sz){
                                        if(!method2.invoke(((T) obj1), null).toString().equals(method1.invoke(((T) obj2), null).toString())){
                                            if(tt.contains("int")){
                                                if(Integer.parseInt(method1.invoke(((T) obj1), null).toString())>Integer.parseInt(method2.invoke(((T) obj2), null).toString())){
                                                    retVal =1;
                                                    break;
                                                }else if(Integer.parseInt(method1.invoke(((T) obj1), null).toString())<Integer.parseInt(method2.invoke(((T) obj2), null).toString())){
                                                    retVal =-1;
                                                    break;
                                                }else{
                                                    retVal=0;
                                                    continue;
                                                }
                                            }else if(tt.contains("double")){
                                                if(Double.parseDouble(method1.invoke(((T) obj1), null).toString())>Double.parseDouble(method2.invoke(((T) obj2), null).toString())){
                                                    retVal =1;
                                                    break;
                                                }else if(Double.parseDouble(method1.invoke(((T) obj1), null).toString())<Double.parseDouble(method2.invoke(((T) obj2), null).toString())){
                                                    retVal =-1;
                                                    break;
                                                }else{
                                                    retVal=0;
                                                    continue;
                                                }
                                            }
                                        }else{
                                            retVal=0;
                                            continue;
                                        }
                                    }else{
                                        if(!method2.invoke(((T) obj1), null).toString().equals(method1.invoke(((T) obj2), null).toString())){
                                            retVal = method2.invoke(((T) obj1), null).toString().compareTo(method1.invoke(((T) obj2), null).toString()); // 正序  
                                            break;
                                        }else{
                                            retVal=0;
                                            continue;
                                        }
                                    }
                                }
                            }  
                        }
                    } catch (Exception e) {  
                        throw new RuntimeException();  
                    }  
                    return retVal;  
                }  
            });  
//            System.out.println("targetList:::::"+Jacksonmethod.tojson(targetList, false));
        } 
 
}