JS实现对数组进行自定义排序

/**
 * 数组排序
 * @param  source 待排序数组
 * @param  orders 排序字段数组
 * @param  type  升序-asc 倒序-desc
 * 调用:var res = $.orderBy(arr, ['OpenTime'], 'desc').results;
 */
$.orderBy = function (source, orders, type) {

    if (source instanceof Array && orders instanceof Array && orders.length > 0) {

        var ordersc = orders.concat([]);
        var sorttype = type || 'asc';
        var results = [];
        var totalSum = {};

        function grouporder(source, orders, totalSum) {

            source.sort(function (a, b) {
                var convertA = a[orders[0]];
                var convertB = b[orders[0]];
                if (typeof convertA == 'string' && typeof convertB == 'string') {
                    if (sorttype.toUpperCase() == 'ASC') {
                        return convertA.localeCompare(convertB);
                    } else {
                        return convertB.localeCompare(convertA);
                    }
                } else {
                    if (sorttype.toUpperCase() == 'ASC') {
                        return convertA - convertB;
                    } else {
                        return convertB - convertA;
                    }
                }
            });

            var groupmap = new Map();
            source.forEach((item) => {
                if (groupmap.has(item[orders[0]])) {
                    groupmap.get(item[orders[0]]).push(item);
                } else {
                    groupmap.set(item[orders[0]], []);
                    groupmap.get(item[orders[0]]).push(item);
                }
            });

            orders.shift();

            for (let [key, val] of groupmap) {

                totalSum[key] = {};
                totalSum[key].name = key;
                totalSum[key].value = val.length;
                if (orders.length == 0) {
                    results = results.concat(val);
                } else {
                    totalSum[key].children = {};
                    var orderscopy = orders.concat([]);
                    grouporder(val, orderscopy, totalSum[key].children);
                }
            }
        }

        grouporder(source, ordersc, totalSum);

        return {
            results: results,
            totalSum: totalSum
        };
    } else {
        return source;
    }
}

  

posted @ 2017-12-25 17:01  大师兄丶  阅读(4729)  评论(0编辑  收藏  举报