实现一个排序器,支持多重排序,直接上代码吧!
import com.google.gson.reflect.TypeToken;
import monkey.common.ObjectMapper;
import monkey.common.Result;
import seagull.data.view.ApiResultData;
import java.util.*;
/**
* @author tmuffamd
* @date 2022/8/8 - 14:27
*/
public class ListComparatorUtils {
public static <E> void sort(List<Map<String, Object>> list, final LinkedHashMap<String, String> sortFields) {
Collections.sort(list, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> a, Map<String, Object> b) {
int ret = 0;
try {
for (Map.Entry<String, String> entry : sortFields.entrySet( )) {
Object value1 = a.get(entry.getKey( ));
Object value2 = b.get(entry.getKey( ));
if (value1 instanceof Number && value2 instanceof Number) {
if (entry.getValue( ).equals("asc")) {
ret = Double.valueOf(value1.toString()).compareTo(Double.valueOf(value2.toString()));
} else {
ret = Double.valueOf(value2.toString()).compareTo(Double.valueOf(value1.toString()));
}
}else{
if (entry.getValue( ).equals("asc")) {
ret = value1.toString().compareTo(value2.toString());
} else {
ret = value2.toString().compareTo(value1.toString());
}
}
if (0 != ret) {break;}
}
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
});
}
public static Comparator<Map<String, Object>> buildComparator(LinkedHashMap<String, String> sortFields) {
Comparator<Map<String, Object>> comparator = null;
for (Map.Entry<String, String> entry : sortFields.entrySet()) {
String sort = entry.getValue( );
String field = entry.getKey( );
if (comparator == null) {
if ("desc".equals(sort)) {
comparator = Comparator.comparing((Map<String, Object> map) -> map.get(field).toString(),Comparator.reverseOrder());
}else{
comparator = Comparator.comparing((Map<String, Object> map) -> map.get(field).toString());
}
} else {
if ("desc".equals(sort)) {
comparator = comparator.thenComparing((Map<String, Object> map) -> Double.valueOf(map.get(entry.getKey()).toString()),Comparator.reverseOrder());
}else{
comparator = comparator.thenComparing((Map<String, Object> map) -> Double.valueOf(map.get(entry.getKey()).toString()));
}
}
}
return comparator;
}
public static void main(String[] args) {
String resultJSON = "{\"statusCode\":600,\"data\":{\"total\":0,\"pageNum\":1,\"pageSize\":10,\"list\":[{\"rank\":1,\"proportion\":0.5307,\"cashier_id\":\"100\",\"charge_num\":72,\"charge_money\":43964.67},{\"rank\":3,\"proportion\":0.062,\"cashier_id\":\"989192982705999872\",\"charge_num\":95,\"charge_money\":13696.0},{\"rank\":3,\"proportion\":0.1566,\"cashier_id\":\"989192982705999872\",\"node_code\":\"0001\",\"charge_num\":5,\"charge_money\":12973.0},{\"rank\":3,\"proportion\":0.062,\"cashier_id\":\"989192982705999872\",\"charge_num\":4,\"charge_money\":5140.55},{\"rank\":5,\"proportion\":0.0546,\"cashier_id\":\"989200563587518464\",\"charge_num\":48,\"charge_money\":4527.0},{\"rank\":6,\"proportion\":0.0149,\"cashier_id\":\"988826457948034048\",\"charge_num\":5,\"charge_money\":1237.13},{\"rank\":7,\"proportion\":0.0118,\"cashier_id\":\"989192982705999872\",\"node_code\":\"0003\",\"charge_num\":1,\"charge_money\":981.0},{\"rank\":8,\"proportion\":0.0018,\"cashier_id\":\"1004393987248771072\",\"charge_num\":4,\"charge_money\":151.51999999999998},{\"rank\":9,\"proportion\":0.0016,\"cashier_id\":\"988805509475734528\",\"charge_num\":2,\"charge_money\":134.9},{\"rank\":10,\"proportion\":3.0,\"cashier_id\":\"949254323525686272\",\"charge_num\":1,\"charge_money\":26.0},{\"rank\":11,\"proportion\":0.0,\"cashier_id\":\"10070\",\"charge_num\":0,\"charge_money\":0.0},{\"rank\":12,\"proportion\":0.0,\"cashier_id\":\"0\",\"charge_num\":0,\"charge_money\":0.0}]}}";
Result<ApiResultData> result = ObjectMapper.fromJson(resultJSON, new TypeToken<Result<ApiResultData>>(){}.getType());
List<Map<String, Object>> list = result.getData().getList();
list.forEach(item-> System.out.println(item ));
System.out.println("===============" );
LinkedHashMap<String, String> sortFields = new LinkedHashMap<>();
sortFields.put("rank","desc");
sortFields.put("proportion","desc");
sortFields.put("cashier_id","asc");
sortFields.put("charge_num","asc");
//sort(list,sortFields);
list.sort(buildComparator(sortFields));
list.forEach(item-> System.out.println(item ));
}
}