package com.jd.dashboard.util;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jd.big.data.common.dashboard.model.DeptMonitorStoreVo;
import com.jd.big.data.common.dashboard.model.DeptMonitorVo; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; /**
* 分部门级别的排序的排序
* Created by niewj on 2016/10/20.
*/
public class SortUtil { /**
* 按照部门父子顺序排序:1.父子部门;2.同级别内按照value值倒序
*
* @param list 所有的节点
* @return
*/
public static List<? extends DeptMonitorVo> sortAllDeptLevelList(List<? extends DeptMonitorVo> list) {
if (list == null || list.isEmpty()) {
return list;
} /**
* 1.分成4组
*/
List<DeptMonitorVo> listBu = new ArrayList<>();
List<DeptMonitorVo> listDept1 = new ArrayList<>();
List<DeptMonitorVo> listDept2 = new ArrayList<>();
List<DeptMonitorVo> listDept3 = new ArrayList<>();
for (DeptMonitorVo vo : list) {
if (vo.getBu_id() != null && vo.getDept_id_1() == null) {
listBu.add(vo);
} else if (vo.getBu_id() != null && vo.getDept_id_1() != null && vo.getDept_id_2() == null) {
listDept1.add(vo);
} else if (vo.getBu_id() != null && vo.getDept_id_1() != null && vo.getDept_id_2() != null && vo.getDept_id_3() == null) {
listDept2.add(vo);
} else if (vo.getBu_id() != null && vo.getDept_id_1() != null && vo.getDept_id_2() != null && vo.getDept_id_3() != null) {
listDept3.add(vo);
}
} /**
* 2.每组排序
*/
if (!listBu.isEmpty()) {
Collections.sort(listBu);
}
if (!listDept1.isEmpty()) {
Collections.sort(listDept1);
}
if (!listDept2.isEmpty()) {
Collections.sort(listDept2);
}
if (!listDept3.isEmpty()) {
Collections.sort(listDept3);
} /**
* 3.认亲-认上层节点-追加顺序
*/
List<DeptMonitorVo> result2 = sortParentAndSon3(listDept3, listDept2);
List<DeptMonitorVo> result1 = sortParentAndSon2(result2, listDept1);
List<DeptMonitorVo> result0 = sortParentAndSon1(result1, listBu); return result0;
} /**
* 两层排序,然后返回父亲列表
*
* @param sonList
* @param parentList
*/
private static List<DeptMonitorVo> sortParentAndSon3(List<DeptMonitorVo> sonList, List<DeptMonitorVo> parentList) {
if (sonList.isEmpty()) {
return parentList;
} List<DeptMonitorVo> result = new ArrayList<>(); for (DeptMonitorVo parentVo : parentList) {
// 1. 先加入父亲
result.add(parentVo);
long dept2 = parentVo.getDept_id_2().longValue(); // 2. 再便利子节点中的符合项, 加入
for (DeptMonitorVo sonVo : sonList) {
long sonDept2 = sonVo.getDept_id_2().longValue();
if (dept2 == sonDept2) {
result.add(sonVo);
}
}
} return result;
} /**
* 两层排序,然后返回父亲列表
*
* @param sonList
* @param parentList
*/
private static List<DeptMonitorVo> sortParentAndSon2(List<DeptMonitorVo> sonList, List<DeptMonitorVo> parentList) {
if (sonList.isEmpty()) {
return parentList;
} List<DeptMonitorVo> result = new ArrayList<>(); for (DeptMonitorVo parentVo : parentList) {
result.add(parentVo); // 1. 先加入父亲
long dept1 = parentVo.getDept_id_1().longValue(); for (DeptMonitorVo sonVo : sonList) { // 2. 再便利子节点中的符合项, 加入
long sonDept1 = sonVo.getDept_id_1().longValue();
if (dept1 == sonDept1) {
result.add(sonVo);
}
}
} return result;
} /**
* 两层排序,然后返回父亲列表
*
* @param sonList
* @param parentList
*/
private static List<DeptMonitorVo> sortParentAndSon1(List<DeptMonitorVo> sonList, List<DeptMonitorVo> parentList) {
if (sonList.isEmpty()) {
return parentList;
} List<DeptMonitorVo> result = new ArrayList<>(); for (DeptMonitorVo parentVo : parentList) {
// 1. 先加入父亲
result.add(parentVo);
long dept0 = parentVo.getBu_id().longValue(); // 2. 再便利子节点中的符合项, 加入
for (DeptMonitorVo sonVo : sonList) {
long sonDept0 = sonVo.getBu_id().longValue();
if (dept0 == sonDept0) {
result.add(sonVo);
}
}
} return result;
} public static void main(String[] args) {
String voListString = "[{\"dept_name\":\"3D周边CX部\",\"country_all_day\":1.0,\"bj\":1.0,\"sh\":1.0,\"gz\":1.0,\"cd\":1.0,\"wh\":1.0,\"sy\":1.0,\"xa\":1.0,\"ga\":1.0,\"hz\":1.0,\"bu_id\":2420,\"dept_id_1\":1324,\"dept_id_2\":1334},{\"dept_name\":\"ZNSMCX部\",\"country_all_day\":0.96163,\"bj\":0.9632,\"sh\":0.9677,\"gz\":0.94996,\"cd\":0.96186,\"wh\":0.96438,\"sy\":0.963,\"xa\":0.96552,\"ga\":0.96366,\"hz\":0.97454,\"bu_id\":2420,\"dept_id_1\":1324,\"dept_id_2\":1333},{\"dept_name\":\"SMYYCX部\",\"country_all_day\":0.93846,\"bj\":0.94308,\"sh\":0.94065,\"gz\":0.93518,\"cd\":0.94158,\"wh\":0.92635,\"sy\":0.9362,\"xa\":0.94435,\"ga\":0.93955,\"hz\":0.95217,\"bu_id\":2420,\"dept_id_1\":1324,\"dept_id_2\":1332},{\"dept_name\":\"SM业务部\",\"country_all_day\":0.93419,\"bj\":0.94633,\"sh\":0.94231,\"gz\":0.93303,\"cd\":0.92651,\"wh\":0.91655,\"sy\":0.91778,\"xa\":0.92837,\"ga\":0.93629,\"hz\":0.95856,\"bu_id\":2420,\"dept_id_1\":1324},{\"dept_name\":\"SMPOP及配件CX部\",\"country_all_day\":0.93051,\"bj\":0.9493,\"sh\":0.93414,\"gz\":0.92707,\"cd\":0.92507,\"wh\":0.91526,\"sy\":0.91529,\"xa\":0.93691,\"ga\":0.92456,\"hz\":0.95797,\"bu_id\":2420,\"dept_id_1\":1324,\"dept_id_2\":1335},{\"dept_name\":\"平板CX部\",\"country_all_day\":0.90506,\"bj\":0.91987,\"sh\":0.92736,\"gz\":0.92255,\"cd\":0.87691,\"wh\":0.86422,\"sy\":0.85504,\"xa\":0.85542,\"ga\":0.91938,\"hz\":0.94745,\"bu_id\":2420,\"dept_id_1\":1324,\"dept_id_2\":1776},{\"dept_name\":\"电子书\",\"country_all_day\":1.0,\"bj\":1.0,\"sh\":1.0,\"gz\":1.0,\"cd\":1.0,\"wh\":1.0,\"sy\":1.0,\"xa\":1.0,\"bu_id\":2420,\"dept_id_1\":31,\"dept_id_2\":43},{\"dept_name\":\"自营图书CX部\",\"country_all_day\":0.96973,\"bj\":0.9522,\"sh\":0.97175,\"gz\":0.97694,\"cd\":0.977,\"wh\":0.97683,\"sy\":0.97297,\"xa\":0.97296,\"bu_id\":2420,\"dept_id_1\":31,\"dept_id_2\":41},{\"dept_name\":\"图书音像YW部\",\"country_all_day\":0.968,\"bj\":0.95127,\"sh\":0.97,\"gz\":0.97537,\"cd\":0.97542,\"wh\":0.97583,\"sy\":0.96917,\"xa\":0.96876,\"bu_id\":2420,\"dept_id_1\":31},{\"dept_name\":\"音像CX部\",\"country_all_day\":0.89063,\"bj\":0.90068,\"sh\":0.8967,\"gz\":0.91775,\"cd\":0.91461,\"wh\":0.92825,\"sy\":0.77064,\"xa\":0.70671,\"bu_id\":2420,\"dept_id_1\":31,\"dept_id_2\":42},{\"dept_name\":\"办公CX部\",\"country_all_day\":0.95925,\"bj\":0.97993,\"sh\":0.95771,\"gz\":0.95925,\"cd\":0.95966,\"wh\":0.95505,\"sy\":0.95023,\"xa\":0.95304,\"ga\":0.94573,\"hz\":0.96364,\"bu_id\":2420,\"dept_id_1\":30,\"dept_id_2\":39},{\"dept_name\":\"外设CX部\",\"country_all_day\":0.94805,\"bj\":0.95503,\"sh\":0.94445,\"gz\":0.94828,\"cd\":0.94679,\"wh\":0.93955,\"sy\":0.94585,\"xa\":0.94658,\"ga\":0.95417,\"hz\":0.95216,\"bu_id\":2420,\"dept_id_1\":30,\"dept_id_2\":40},{\"dept_name\":\"电脑配件CX部\",\"country_all_day\":0.93659,\"bj\":0.9486,\"sh\":0.93087,\"gz\":0.92917,\"cd\":0.94604,\"wh\":0.94384,\"sy\":0.93015,\"xa\":0.93837,\"ga\":0.93589,\"hz\":0.94406,\"bu_id\":2420,\"dept_id_1\":30,\"dept_id_2\":336},{\"dept_name\":\"电脑办公业务部\",\"country_all_day\":0.91942,\"bj\":0.94834,\"sh\":0.91891,\"gz\":0.90477,\"cd\":0.91616,\"wh\":0.89693,\"sy\":0.92615,\"xa\":0.92688,\"ga\":0.93079,\"hz\":0.92965,\"bu_id\":2420,\"dept_id_1\":30},{\"dept_name\":\"整机CX部\",\"country_all_day\":0.85047,\"bj\":0.91414,\"sh\":0.85749,\"gz\":0.81637,\"cd\":0.83107,\"wh\":0.79688,\"sy\":0.87217,\"xa\":0.87578,\"ga\":0.88881,\"hz\":0.87836,\"bu_id\":2420,\"dept_id_1\":30,\"dept_id_2\":37},{\"dept_name\":\"直供业务部\",\"country_all_day\":0.84758,\"bj\":0.83333,\"sh\":0.84127,\"gz\":0.80693,\"cd\":0.94118,\"wh\":0.87619,\"sy\":0.89474,\"xa\":0.72727,\"ga\":0.89209,\"hz\":0.66667,\"bu_id\":2420,\"dept_id_1\":1,\"dept_id_2\":1553},{\"dept_name\":\"通讯业务二部\",\"country_all_day\":0.76592,\"bj\":0.77973,\"sh\":0.74857,\"gz\":0.75667,\"cd\":0.77958,\"wh\":0.76166,\"sy\":0.7784,\"xa\":0.77247,\"ga\":0.77877,\"hz\":0.76151,\"bu_id\":2420,\"dept_id_1\":1,\"dept_id_2\":738},{\"dept_name\":\"TX业务部\",\"country_all_day\":0.65548,\"bj\":0.64468,\"sh\":0.62067,\"gz\":0.67024,\"cd\":0.67957,\"wh\":0.6631,\"sy\":0.65866,\"xa\":0.66686,\"ga\":0.65447,\"hz\":0.62753,\"bu_id\":2420,\"dept_id_1\":1},{\"dept_name\":\"运营商业务部\",\"country_all_day\":0.55414,\"bj\":0.52975,\"sh\":0.55859,\"gz\":0.53727,\"cd\":0.69676,\"wh\":0.50726,\"sy\":0.51485,\"xa\":0.56237,\"ga\":0.51848,\"hz\":0.55759,\"bu_id\":2420,\"dept_id_1\":1,\"dept_id_2\":1524},{\"dept_name\":\"通讯业务一部\",\"country_all_day\":0.54539,\"bj\":0.52234,\"sh\":0.50463,\"gz\":0.57716,\"cd\":0.57954,\"wh\":0.56136,\"sy\":0.54289,\"xa\":0.55968,\"ga\":0.52331,\"hz\":0.51473,\"bu_id\":2420,\"dept_id_1\":1,\"dept_id_2\":727},{\"dept_name\":\"2D事业部\",\"country_all_day\":0.82657,\"bj\":0.84881,\"sh\":0.82391,\"gz\":0.81995,\"cd\":0.83711,\"wh\":0.81759,\"sy\":0.83341,\"xa\":0.83206,\"ga\":0.81426,\"hz\":0.82026,\"bu_id\":2420}]";
List<DeptMonitorStoreVo> result = new Gson().fromJson(voListString, new TypeToken<List<DeptMonitorStoreVo>>() {
}.getType());
List<? extends DeptMonitorVo> result0 = SortUtil.sortAllDeptLevelList(result);
System.out.println(new Gson().toJson(result0));
}
}

自定义泛型方法, 三级排序, low版,待升级的更多相关文章

  1. 重写Oracle的wm_concat函数,自定义分隔符、排序

    oracle中,wm_concat函数是一个聚合函数,和mysql中的group_concat函数类似,不过group_concat函数比较强大,可以定义分隔符和排序,当然所谓强大是相对的,这里假使我 ...

  2. win10预览版9926升级10049操作步骤

    文章转自:豆豆系统收藏备用 win10预览版系统安装的用户非常多,现在最新版本已经到了10049,但是之前很多装了9926版本或者10041版本的同学在通过系统自动更新的时候,且发现,微软官方提供的速 ...

  3. MapReduce自定义二次排序流程

    每一条记录开始是进入到map函数进行处理,处理完了之后立马就入自定义分区函数中对其进行分区,当所有输入数据经过map函数和分区函数处理完之后,就调用自定义二次排序函数对其进行排序. MapReduce ...

  4. Java基础知识强化之集合框架笔记69:Collections类之ArrayList存储自自定义对象并排序的案例

    1. ArrayList存储自自定义对象并排序的案例: ArrayList存储自自定义对象,并使用Collections对ArrayList存储基本包装类的元素排序. 2. 代码实现: (1)Stud ...

  5. Hadoop学习之自定义二次排序

    一.概述    MapReduce框架对处理结果的输出会根据key值进行默认的排序,这个默认排序可以满足一部分需求,但是也是十分有限的.在我们实际的需求当中,往 往有要对reduce输出结果进行二次排 ...

  6. 功能测试很low?不能升级到高级测试工程师?

    功能测试很low?不能升级到高级测试工程师? 功能测试很low?功能测试很简单?功能测试就是黑盒测试?功能测试没有技术含量?功能测试工资低?只会功能测试没有竞争力?功能测试这活初中生都可以干?功能测试 ...

  7. mac版chrome升级到Version 65.0.3325.18后无法打开百度bing搜狗

    mac版本chrome升级到Version 65.0.3325.18后发现突然无法访问百度,搜狗,bing,神马等一系列的国内搜索引擎网站.连百度的儿子们比如知道,百度百科都无法访问. 1.首先想到的 ...

  8. 【转载】C#中自定义Sort的排序规则IComparable接口

    C#中的List集合在排序的时候,如果不使用Lambda表达式进行排序的话,一般调用Sort()方法进行排序,如果希望Sort()方法排序后的结果跟我们预想的效果一致或者按照我们自定义的规则排序,则需 ...

  9. java泛型-自定义泛型方法与类型推断总结

    下面是自定义泛型方法的练习: package com.mari.generic; import java.util.ArrayList; import java.util.Collection; im ...

随机推荐

  1. Go语言中的常见的几个坑

    目录 1.for range 2.defer与闭包 3.map内存溢出 4.协程泄漏 5.http手动关闭 记录一下日常中遇到的几个坑,加深一下印象. 1.for range 这个是比较常见的问题了, ...

  2. day65:nginx代理&nginx负载均衡

    目录 1.nginx代理 2.nginx代理与配置 3.nginx负载均衡调度多web节点(静态页面) 4.nginx负载均衡调度多应用节点(blog) 5.nginx_proxy + web应用节点 ...

  3. shell-变量输入内置read命令详解

    1. shell变量的输入 shell变量除了可以直接赋值或脚本传参外,还可以使用read命令从标准输入得. [语法格式] read [参数] [变量名] [常用参数] -p:指定读取值时的提示符: ...

  4. (OK) Android内核(4.9)集成最新版MPTCP---成功

    Android内核(4.9)集成最新版MPTCP---成功

  5. 多台centos7服务器实现SSH免密登陆

    一.环境 centos7.x 三台 node1.node2.node3 二.实现免密登陆 2.1.node1上,生成公钥与私钥 [root@node1 ~]# ssh-keygen Generatin ...

  6. 第4天 | 12天搞定Python,基础语法(下)

    为了方便你的学习,减轻负重,我特意将基础语法分成上下两部分.希望你喜欢这种方式,如果不喜欢,你可以跟我说,反正我是不会改的,哈哈~~. 如果上部分,你还没看的话,先去看<第4天 | 12天搞定P ...

  7. iproute2工具

    iproute2工具介绍 iproute2是linux下管理控制TCP/IP网络和流量控制的新一代工具包,出现目的是替代老工具链net-tools.net-tools是通过procfs(/proc)和 ...

  8. 【C语言入门】"为什么这个又错了啊"来自编程初学者常见错误合辑!

    C语言的最大特点是:功能强,使用方便灵活. C编译的程序对语法检查并不象其它高级语言那么严格,这就给编程人员留下"灵活的 余地",但还是由于这个灵活给程序的调试带来了许多不便,尤其 ...

  9. springboot打成jar包和war包的两种方式,并引入第三方jar包!

    springboot打成jar包和war包的两种方式,并引入第三方jar包! 首先把需要引入的第三方jar包引入到本地项目中,在引用的模块下加一个lib文件夹 一.打成jar包 1.修改pom文件里的 ...

  10. win10使用U盘安装Linux系统教程

    win10安装Linux系统详细教程 目前想要再Windows系统上安装Linux系统有三种方式:其一是安装在虚拟机上(VMWare或者VirtualBox),其二是使用win10最新支持的Linux ...