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;
}
}

tempcode排序的更多相关文章

  1. javascript中的Array对象 —— 数组的合并、转换、迭代、排序、堆栈

    Array 是javascript中经常用到的数据类型.javascript 的数组其他语言中数组的最大的区别是其每个数组项都可以保存任何类型的数据.本文主要讨论javascript中数组的声明.转换 ...

  2. iOS可视化动态绘制八种排序过程

    前面几篇博客都是关于排序的,在之前陆陆续续发布的博客中,我们先后介绍了冒泡排序.选择排序.插入排序.希尔排序.堆排序.归并排序以及快速排序.俗话说的好,做事儿要善始善终,本篇博客就算是对之前那几篇博客 ...

  3. JavaScript实现常用的排序算法

    ▓▓▓▓▓▓ 大致介绍 由于最近要考试复习,所以学习js的时间少了 -_-||,考试完还会继续的努力学习,这次用原生的JavaScript实现以前学习的常用的排序算法,有冒泡排序.快速排序.直接插入排 ...

  4. [C#][算法] 用菜鸟的思维学习算法 -- 马桶排序、冒泡排序和快速排序

    用菜鸟的思维学习算法 -- 马桶排序.冒泡排序和快速排序 [博主]反骨仔 [来源]http://www.cnblogs.com/liqingwen/p/4994261.html  目录 马桶排序(令人 ...

  5. 算法与数据结构(十三) 冒泡排序、插入排序、希尔排序、选择排序(Swift3.0版)

    本篇博客中的代码实现依然采用Swift3.0来实现.在前几篇博客连续的介绍了关于查找的相关内容, 大约包括线性数据结构的顺序查找.折半查找.插值查找.Fibonacci查找,还包括数结构的二叉排序树以 ...

  6. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

  7. 使用po模式读取豆瓣读书最受关注的书籍,取出标题、评分、评论、题材 按评分从小到大排序并输出到txt文件中

    #coding=utf-8from time import sleepimport unittestfrom selenium import webdriverfrom selenium.webdri ...

  8. javascript排序

    利用array中的sort()排序 w3cfunction sortNumber(a,b) { return a - b } var arr = new Array(6) arr[0] = " ...

  9. iOS自定义model排序

    在开发过程中,可能需要按照model的某种属性排序. 1.自定义model @interface Person : NSObject @property (nonatomic,copy) NSStri ...

  10. Lucene4.4.0 开发之排序

    排序是对于全文检索来言是一个必不可少的功能,在实际运用中,排序功能能在某些时候给我们带来很大的方便,比如在淘宝,京东等一些电商网站我们可能通过排序来快速找到价格最便宜的商品,或者通过排序来找到评论数最 ...

随机推荐

  1. [FAQ] chrome.runtime.onMessage 问题, Unchecked runtime.lastError: The message port closed before a response was received

    // quasar background-hook.js chrome.runtime.onMessage.addListener(function (request, sender, sendRes ...

  2. 数据分析之重要模块pandas

    1.简介 基于Numpy构建 pandas的出现,让Python语言成为使用最广泛而且强大的数据分析环境之一 pandas的主要功能 - 具备诸多功能的两大数据结构 Series.DataFrame( ...

  3. Jetpack Compose(6)——动画

    目录 一.低级别动画 API 1.1 animate*AsState 1.2 Animatable 1.3 Transition 动画 1.3.1 updateTransition 1.3.2 cre ...

  4. 一个用Python将视频变为表情包的工具

    这是一个将视频转变为表情包的工具,现实生活中当我们看到一段搞笑的视频,我们可以将这段视频喂给这段程序,生成gif表情包,这样就可以用来舍友斗图了 1.一些限制 1.这个程序不能转化超过15秒以上的视频 ...

  5. ES_CCS/R(三):跨集群复制 Cross-cluster replication(CCR)

    跨集群复制(CCR)功能支持将远程集群中的索引复制到本地集群. 可以在一些常见的生产用例中使用此功能: 灾难恢复(DR)/高可用性(HA):如果主群集发生故障,则进行灾难恢复. 辅助群集可以用作热备份 ...

  6. DNS(4) -- dns功能实现-配置正向解析和反向解析以及DNS递归查询示例

    目录 1 DNS配置示例 1.1 DNS解析类型 1.2 配置正向解析 1.3 配置反向解析 1.4 DNS递归查询 1.4.1 开启递归查询 1.4.2 关闭递归查询 1 DNS配置示例 1.1 D ...

  7. .net core 微信支付-微信小程序支付(服务端C#代码)

    前言 前段时间研究了下微信支付-小程序支付的功能.但微信支付文档中关于.net C#的语言的sdk没有,只有java go 和php版本的,当然社区也有很多已经集成好的微信支付.net core sd ...

  8. 计算机网络基础 — Linux 虚拟路由器

    目录 文章目录 目录 前文列表 前言 Neutron L3 agent 概述 L3 agent的配置 虚拟路由器实现原理 总结 前文列表 <计算机网络基础 - 以太网> <计算机网络 ...

  9. SimpleHTTPServer模块详解

    SimpleHTTPServer实现文件的展示和下载 可以用python2.7直接启动一个进程.以命令执行的当前目录为页面根目录,如果不存在index.html,默认展示当前目录的所有文件. pyth ...

  10. linux file命令查看文件类型

    在linux系统中,linux是不根据后缀名识别文件类型的,所以使用file命令查看文件的类型. [root@node5 ~]# file /etc/shadow /etc/shadow: ASCII ...