`

自定义泛型方法, 三级排序, low版,待升级

    博客分类:
  • Java
 
阅读更多
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));
    }
}

 

0
1
分享到:
评论

相关推荐

    .NET之美:.NET关键技术深入分析

    2.1.3泛型方法 2.2泛型与集合类型 2.2.1 避免隐式的装箱和拆箱 2.2.2编译时的类型安全 2.2.3使用泛型的一个小技巧 2.3本章小结 第3章C#中的委托和事件 3.1理解委托 3.1.1 将方法作为方法的参数 3.1.2将方法...

    疯狂JAVA讲义

    8.4.4 泛型方法与方法重载 309 8.5 擦除和转换 310 8.6 泛型与数组 311 8.7 本章小结 313 第9章 与运行环境交互 314 9.1 与用户互动 315 9.1.1 运行Java程序的参数 315 9.1.2 使用Scanner获取键盘输入 316 ...

    Java开发技术大全 电子版

    Java开发技术大全 电子版 第1篇Java基础知识入门. 第1章Java的开发运行环境2 1.1Java的运行环境与虚拟机2 1.2Java的开发环境4 1.2.1JDK的安装4 1.2.2如何设置系统环境变量6 1.2.3编译命令的使用8 1.2.4解释...

    Java开发实战1200例(第1卷).(清华出版.李钟尉.陈丹丹).part3

    实例185 自定义泛型化数组类 235 实例186 泛型方法与数据查询 236 实例187 泛型化方法与最小值 238 实例188 泛型化接口与最大值 239 实例189 使用通配符增强泛型 240 实例190 泛型化的折半查找法 241 第9章 编程常用...

    javaSE代码实例

    13.3.2 编写方法链以及StringBuffer类的重要方法 255 13.4 StringBuilder类 258 13.5 正则表达式 259 13.5.1 正则表达式的基本语法 259 13.5.2 Pattern类简介 262 13.5.3 Matcher类简介 263 13.5.4 ...

    asp.net知识库

    可按任意字段排序的分页存储过程(不用临时表的方法,不看全文会后悔) 常用sql存储过程集锦 存储过程中实现类似split功能(charindex) 通过查询系统表得到纵向的表结构 将数据库表中的数据生成Insert脚本的存储过程!!! ...

    Visual.Basic.2010.&.NET4.高级编程(第6版)-文字版.pdf

    高级编程(第6版)-文字版.pdf 第I部分 语言结构和环境 第1章 visual studio 2010 3 1.1 visual studio 2010:从express到ultimate的各种版本 4 1.2 visual basic的关键字和语法 7 1.2.1 控制台应用程序 10 ...

    C#开发实例大全(基础卷).软件开发技术联盟(带详细书签) PDF 下载

    实例104 使用Sort方法对数组进行快速排序 124 实例105 反转数组中元素的顺序 125 4.3 常用集合的使用 126 实例106 向班级集合中添加学生信息 126 实例107 使用哈希表对XML文件进行查询 127 实例108 计算两个矩形矩阵...

    明日科技C#开发入门及项目实战

    实例050 使用sort方法对数组进行快速排序 实例051 按指定条件在数组中检索元素 实例052 反转数组中元素的顺序 实例053 使用希尔排序法对一维数组进行排序 实例054 向班级集合中添加学生信息 实例055 不改变长度删除...

    LINQ 实战 1/11

    第二、三、四部分详细介绍了LINQ的三大主要功能LINQ to Objects、LINQ to SQL、LINQ to XML,第五部分研究了LINQ的可扩展性、在应用程序中使用各种LINQ功能的方法以及LINQ to DataSet。  本书适合于使用C#和VB.NET...

    LINQ 实战 3/11

    第二、三、四部分详细介绍了LINQ的三大主要功能LINQ to Objects、LINQ to SQL、LINQ to XML,第五部分研究了LINQ的可扩展性、在应用程序中使用各种LINQ功能的方法以及LINQ to DataSet。  本书适合于使用C#和VB.NET...

Global site tag (gtag.js) - Google Analytics