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