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

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

猜你喜欢

转载自niewj.iteye.com/blog/2331987