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. mysql DISTINCT选取多个字段,获取distinct后的行信息

    背景:      a表保存关联关系,通过ACode 获取该关系中的所有 BCode, 并获取所有Bcode-max(Bvrsn)的信息  Bnm 表a 表b          循序渐进:        ...

  2. 用网桥和veth实现容器的桥接模式

    原理图如下 具体命令先不写了,有时间再写,主要还是用的上一篇说的知识.

  3. 解决React前端在开发环境的跨域问题

    在前后端分离的分布式架构中,跨域是一道无法绕过去的门槛,众所周知,生产环境上解决跨域最便捷的方式是使用Nginx来处理,那么,在本地开发环境又该如何处理呢? React框架里处理跨域问题,可以使用ht ...

  4. linunx常用命令综合

    # linux常用命令exsi 6.5虚拟化系统命令大全 https://www.runoob.com/linux/linux-command-manual.html# sudo -i 设置切换无密码 ...

  5. go正则

    package main import ( "fmt" "regexp" ) func main() { context1 := "3.14 1231 ...

  6. nginx集群:nginx配置负载均衡集群(nginx1.18.0)

    一,nginx的负载均衡集群的特点: 1,nginx集群和lvs的不同? lvs集群:工作在第4层(传输层) nginx集群:工作在第7层(应用层) lvs集群:性能更强 nginx集群:功能更强:可 ...

  7. html中object标签

    首先将这个强大web页面打印例子(pintTest.html)贴上来. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional ...

  8. eclipse快捷键(Mac版)整理

    eclipse快捷键(Mac版)整理 前言: 最近在学习JDBC,老师用的是eclipse,对于习惯了IDEA的我来说,没有了快捷键,效率明显下降. 我认为,开发工具的熟练使用,也是程序员必备的一项技 ...

  9. python3配置socks5代理进行爬取

    一.代码 #!/usr/bin/python # -*- coding: UTF-8 -*- import requests import socket import socks SOCKS5_PRO ...

  10. Scrapy中get和extract_first的区别

    在scrapy中,从xpath中取得selector对象后,需要取出需要的数据. 使用get以及getall获取的是带标签的数据 比如 <p>这是一段文字</p> 如果用get ...