tempcode排序
package com.hsy;
import com.alibaba.fastjson.JSON;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.math.BigDecimal;
import java.util.*;
public class Test2 {
public static void main(String[] args) {
Map data = getData();
// String rout = "data>cardList^pltCardNmbr";
String rout = "data>accountList^cmCardNmbr,cmOrgNmbr";
String[] split = rout.split("\\^");
listSort(data, rout);
String json = JSON.toJSONString(data);
System.out.println(rout.indexOf('>'));
}
// data>accountList^acNo,acOrg>Int
static void listSort(Map result, String route) {
if (CollectionUtils.isEmpty(result) || route == null || StringUtils.isEmpty(route.trim()))
return;
//格式化移除空格
route = route.replaceAll(" ", "");
int subIndex = route.indexOf(">");
//无下级key配置,退出
// if (subIndex<0)
// return;
String subRoute = subIndex < 0 ? "" : route.substring(subIndex + 1);
//分解排序关键字
String currRout = route.substring(0, subIndex < 0 ? route.length() : subIndex);
String keyName = currRout.contains("^") ? currRout.split("\\^")[0] : currRout;
//若获取类型为Map
Object subObject = result.get(keyName);
if (subObject instanceof Map) {
//route缩减再递归
if (subIndex > -1) {
listSort((Map) subObject, subRoute);
}
} else if (subObject instanceof List) {
//info
if (!currRout.contains("^"))
return;
String[] split = currRout.split("\\^");
//list类型排序配置错误
if ("".equals(split[1]))
return;
String[] split2 = split[1].split(",");
String[] objects = filterArrEmpty(split2);
//先排序,route缩减,再遍历递归
sortMapList((List) subObject, "", objects);
//递归遍历每个子项
if (subIndex > -1) {
((List) subObject).forEach(info -> {
listSort((Map) info, subRoute);
});
}
}
}
static String[] filterArrEmpty(String[] arr){
List<String> list = new ArrayList<>();
for (String s : arr) {
if (!"".equals(s)){
list.add(s);
}
}
String[] res = new String[list.size()];
String[] strings = list.toArray(res);
return strings;
}
/**
* 将List<Map<String, Object>>中的元素按照其内部Map中的若干字段排序
*
* @param list
* @param compareFields
* @param order
*/
public static void sortMapList(List<Map<String, Object>> list, String order, String... compareFields) {
Comparator<Map<String, Object>> comparetor = new Comparator<Map<String, Object>>() {
private int defineCompare(Object value1, Object value2) {
if (value1 instanceof String && value2 instanceof String) {
return ((String) value1).compareTo((String) value2);
} else if (value1 instanceof Integer && value2 instanceof Integer) {
return ((Integer) value1).compareTo((Integer) value2);
} else if (value1 instanceof Long && value2 instanceof Long) {
return ((Long) value1).compareTo((Long) value2);
} else if (value1 instanceof Double && value2 instanceof Double) {
return ((Double) value1).compareTo((Double) value2);
} else if (value1 instanceof Float && value2 instanceof Float) {
return ((Float) value1).compareTo((Float) value2);
} else if (value1 instanceof BigDecimal && value2 instanceof BigDecimal) {
return ((BigDecimal) value1).compareTo((BigDecimal) value2);
} else {
return -1;
}
}
@Override
public int compare(Map<String, Object> map1, Map<String, Object> map2) {
if (compareFields == null || compareFields.length == 0) {
return 0;
}
int compareResult = 0;
for (int i = 0; i < compareFields.length; i++) {
String compareField = compareFields[i];
Object map1Value = map1.get(compareField);
Object map2Value = map2.get(compareField);
compareResult = defineCompare(map1Value, map2Value);
if (compareResult != 0) {
break;
}
}
return compareResult;
}
};
if ("DESC".equalsIgnoreCase(order)) {
//降序排序
Collections.sort(list, Collections.reverseOrder(comparetor));
} else {
//升序排序
Collections.sort(list, comparetor);
}
}
static Map getData() {
Map map = JSON.parseObject("{\n" +
"\t\"msg\": \"OK\",\n" +
"\t\"code\": \"200\",\n" +
"\t\"data\": {\n" +
"\t\t\"cus2OfficePhoneZone\": \"456 \",\n" +
"\t\t\"crIc\": \"01431229196411103226 \",\n" +
"\t\t\"cus2EmailAddr1\": \"test@tet.qq.com \",\n" +
"\t\t\"crShortName\": \"小河 \",\n" +
"\t\t\"crIdExpDte\": \"20500101\",\n" +
"\t\t\"crIncome\": \"100\",\n" +
"\t\t\"crName2\": \"\",\n" +
"\t\t\"cus2Handphone1\": \"013283408074\",\n" +
"\t\t\"cus2OfficePhoneNoExt\": \"1234 \",\n" +
"\t\t\"crDteBirth\": \"19840116\",\n" +
"\t\t\"cus2HouseOwner\": \"2\",\n" +
"\t\t\"cus2OfficePhoneNo\": \"87003694 \",\n" +
"\t\t\"cus2Education\": \"06\",\n" +
"\t\t\"crName1\": \"小河\",\n" +
"\t\t\"cus2HomePhoneZone\": \"0571\",\n" +
"\t\t\"cus2OfficePhoneCountry\": \"0086\",\n" +
"\t\t\"crIdEffDte\": \"20100101\",\n" +
"\t\t\"crCity\": \" \",\n" +
"\t\t\"cardList\": [\n" +
"\t\t\t{\n" +
"\t\t\t\t\"cardActivationFlag\": \"Y\",\n" +
"\t\t\t\t\"pltBlockCode\": \" \",\n" +
"\t\t\t\t\"pltActivationCd\": \"1\",\n" +
"\t\t\t\t\"pltDteLstCardExpire\": \"738\",\n" +
"\t\t\t\t\"pltCustomerId\": \"199471027\",\n" +
"\t\t\t\t\"pltRelationship\": \"C\",\n" +
"\t\t\t\t\"pltAccountType0\": \"200\",\n" +
"\t\t\t\t\"pltCardNmbr\": \"633\",\n" +
"\t\t\t\t\"pltAccountOrg0\": \"156\",\n" +
"\t\t\t\t\"pltTypeNmbr\": \"897\",\n" +
"\t\t\t\t\"pltDteCardIssue\": \"20380605\",\n" +
"\t\t\t\t\"pltAccountOrg1\": \"840\",\n" +
"\t\t\t\t\"pltAccountNmbr0\": \"199471027200001\",\n" +
"\t\t\t\t\"pltAccountNmbr1\": \"199471027200001\",\n" +
"\t\t\t\t\"pltChipCardFlag\": \"3\",\n" +
"\t\t\t\t\"pltDteCardExpire\": \"743\",\n" +
"\t\t\t\t\"pltLastActivationCd\": \"0\",\n" +
"\t\t\t\t\"pltStatus\": \"2\"\n" +
"\t\t\t},\n" +
"\t\t\t{\n" +
"\t\t\t\t\"cardActivationFlag\": \"Y\",\n" +
"\t\t\t\t\"pltBlockCode\": \" \",\n" +
"\t\t\t\t\"pltActivationCd\": \"0\",\n" +
"\t\t\t\t\"pltDteLstCardExpire\": \"843\",\n" +
"\t\t\t\t\"pltCustomerId\": \"199471027\",\n" +
"\t\t\t\t\"pltRelationship\": \"P\",\n" +
"\t\t\t\t\"pltAccountType0\": \"1\",\n" +
"\t\t\t\t\"pltCardNmbr\": \"622\",\n" +
"\t\t\t\t\"pltAccountOrg0\": \"156\",\n" +
"\t\t\t\t\"pltTypeNmbr\": \"783\",\n" +
"\t\t\t\t\"pltDteCardIssue\": \"20401101\",\n" +
"\t\t\t\t\"pltAccountOrg1\": \"840\",\n" +
"\t\t\t\t\"pltAccountNmbr0\": \"199471027001001\",\n" +
"\t\t\t\t\"pltAccountNmbr1\": \"199471027001001\",\n" +
"\t\t\t\t\"pltChipCardFlag\": \"3\",\n" +
"\t\t\t\t\"pltDteCardExpire\": \"943\",\n" +
"\t\t\t\t\"pltLastActivationCd\": \"1\",\n" +
"\t\t\t\t\"pltStatus\": \"2\"\n" +
"\t\t\t},\n" +
"\t\t\t{\n" +
"\t\t\t\t\"cardActivationFlag\": \"Y\",\n" +
"\t\t\t\t\"pltBlockCode\": \" \",\n" +
"\t\t\t\t\"pltActivationCd\": \"1\",\n" +
"\t\t\t\t\"pltDteLstCardExpire\": \"433\",\n" +
"\t\t\t\t\"pltCustomerId\": \"199471027\",\n" +
"\t\t\t\t\"pltRelationship\": \"P\",\n" +
"\t\t\t\t\"pltAccountType0\": \"1\",\n" +
"\t\t\t\t\"pltCardNmbr\": \"477\",\n" +
"\t\t\t\t\"pltAccountOrg0\": \"156\",\n" +
"\t\t\t\t\"pltTypeNmbr\": \"783\",\n" +
"\t\t\t\t\"pltDteCardIssue\": \"20330305\",\n" +
"\t\t\t\t\"pltAccountOrg1\": \"840\",\n" +
"\t\t\t\t\"pltAccountNmbr0\": \"199471027001001\",\n" +
"\t\t\t\t\"pltAccountNmbr1\": \"199471027001001\",\n" +
"\t\t\t\t\"pltChipCardFlag\": \"3\",\n" +
"\t\t\t\t\"pltDteCardExpire\": \"438\",\n" +
"\t\t\t\t\"pltLastActivationCd\": \"0\",\n" +
"\t\t\t\t\"pltStatus\": \"2\"\n" +
"\t\t\t}\n" +
"\t\t],\n" +
"\t\t\"cus2CorpName1\": \"老杨公司一段 \",\n" +
"\t\t\"crEuSex\": \"1\",\n" +
"\t\t\"crAcctNbr\": \"199471027\",\n" +
"\t\t\"cus2CorpName2\": \"老杨公司二段 \",\n" +
"\t\t\"accountList\": [\n" +
"\t\t\t{\n" +
"\t\t\t\t\"addrCity\": \"单位省市\",\n" +
"\t\t\t\t\"cmBlockCode\": \" \",\n" +
"\t\t\t\t\"cmType\": \"1\",\n" +
"\t\t\t\t\"cmOrgNmbr\": \"840\",\n" +
"\t\t\t\t\"cmAltCustomerNmbr\": \"0\",\n" +
"\t\t\t\t\"cmCardNmbr\": \"199471027001001\",\n" +
"\t\t\t\t\"addrAddress2\": \"地址是减肥\",\n" +
"\t\t\t\t\"addrAddress1\": \"二二单位地址\",\n" +
"\t\t\t\t\"addrZipCode\": \"315000\",\n" +
"\t\t\t\t\"cmCycle\": \"10\"\n" +
"\t\t\t},\n" +
"\t\t\t{\n" +
"\t\t\t\t\"addrCity\": \"单位省市\",\n" +
"\t\t\t\t\"cmBlockCode\": \" \",\n" +
"\t\t\t\t\"cmType\": \"1\",\n" +
"\t\t\t\t\"cmOrgNmbr\": \"156\",\n" +
"\t\t\t\t\"cmAltCustomerNmbr\": \"0\",\n" +
"\t\t\t\t\"cmCardNmbr\": \"199471027001001\",\n" +
"\t\t\t\t\"addrAddress2\": \"地址是减肥\",\n" +
"\t\t\t\t\"addrAddress1\": \"二二单位地址\",\n" +
"\t\t\t\t\"addrZipCode\": \"315000\",\n" +
"\t\t\t\t\"cmCycle\": \"10\"\n" +
"\t\t\t},\n" +
"\t\t\t{\n" +
"\t\t\t\t\"addrCity\": \"温州市\",\n" +
"\t\t\t\t\"cmBlockCode\": \" \",\n" +
"\t\t\t\t\"cmType\": \"200\",\n" +
"\t\t\t\t\"cmOrgNmbr\": \"156\",\n" +
"\t\t\t\t\"cmAltCustomerNmbr\": \"0\",\n" +
"\t\t\t\t\"cmCardNmbr\": \"199471027000001\",\n" +
"\t\t\t\t\"addrAddress2\": \"大事发生的发生\",\n" +
"\t\t\t\t\"addrAddress1\": \"文昌区\",\n" +
"\t\t\t\t\"addrZipCode\": \"314000\",\n" +
"\t\t\t\t\"cmCycle\": \"15\"\n" +
"\t\t\t},\n" +
"\t\t\t{\n" +
"\t\t\t\t\"addrCity\": \"单位省市\",\n" +
"\t\t\t\t\"cmBlockCode\": \" \",\n" +
"\t\t\t\t\"cmType\": \"200\",\n" +
"\t\t\t\t\"cmOrgNmbr\": \"840\",\n" +
"\t\t\t\t\"cmAltCustomerNmbr\": \"0\",\n" +
"\t\t\t\t\"cmCardNmbr\": \"199471027200002\",\n" +
"\t\t\t\t\"addrAddress2\": \"地址是减肥\",\n" +
"\t\t\t\t\"addrAddress1\": \"二二单位地址\",\n" +
"\t\t\t\t\"addrZipCode\": \"315000\",\n" +
"\t\t\t\t\"cmCycle\": \"20\"\n" +
"\t\t\t}\n" +
"\t\t],\n" +
"\t\t\"crBlock\": \" \",\n" +
"\t\t\"crEuMaritalStatus\": \"2\",\n" +
"\t\t\"crState\": \" \",\n" +
"\t\t\"cus2HomePhoneNo\": \"18918689402 \",\n" +
"\t\t\"addrList\": [\n" +
"\t\t\t{\n" +
"\t\t\t\t\"addrZipCode\": \"313000\"\n" +
"\t\t\t},\n" +
"\t\t\t{\n" +
"\t\t\t\t\"addrZipCode\": \"315000\"\n" +
"\t\t\t},\n" +
"\t\t\t{\n" +
"\t\t\t\t\"addrZipCode\": \"314000\"\n" +
"\t\t\t}\n" +
"\t\t]\n" +
"\t},\n" +
"\t\"list\": null\n" +
"}");
return map;
}
}
最轻松的学习方式莫过于在学习中找到属于自己的乐趣,与诸君共勉
浙公网安备 33010602011771号