js分组排序算法, OrderBy

由于项目中需要对数据进行分组排序,类似于sql中 order by column1,column2...。 

实现的关键是 分组排序,第一个column1,排序完成之后,对其分组,然后按照column2 排序, 在分组。 以此类推。

下面给出实现:

/**
       * [orderBy description]
       * @param  {[type]} source [description]
       * @param  {[type]} orders [description]
       * @param  {[type]} type  {asc, desc}  [description]
       * @return {[type]}        [description]
       */
      orderBy(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;
        }
      },

 

调用:

this.orderBy(arr, ['className'], 'desc')

 

测试数据:

[{
          className: '一班',
          name: '张三',
          sex: '女',
          age: 21,
          aihao: '足球'
        }, {
          className: '一班',
          name: '张三33',
          sex: '女',
          age: 21,
          aihao: '篮球'
        }, {
          className: '一班',
          name: '张三44',
          sex: '女',
          age: 21,
          aihao: '足球'
        }, {
          className: '一班',
          name: '张三55',
          sex: '女',
          age: 21,
          aihao: '篮球'
        }, {
          className: '一班',
          name: '张三1',
          sex: '男',
          age: 21,
          aihao: '篮球'
        }, {
          className: '一班',
          name: '张三2',
          sex: '男',
          age: 21,
          aihao: '足球'
        }, {
          className: '一班',
          name: '张三3',
          sex: '女',
          age: 21,
          aihao: '足球'
        },

        {
          className: '二班',
          name: '李四',
          sex: '女',
          age: 22,
          aihao: '足球'
        }, {
          className: '一班',
          name: '李四1',
          sex: '男',
          age: 23,
          aihao: '篮球'
        }, {
          className: '二班',
          name: '李四2',
          sex: '男',
          age: 24,
          aihao: '足球'
        }, {
          className: '一班',
          name: '李四3',
          sex: '女',
          age: 24,
          aihao: '足球'
        }, {
          className: '二班',
          name: '李四4',
          sex: '女',
          age: 26,
          aihao: '足球'
        }, {
          className: '一班',
          name: '李四5',
          sex: '男',
          age: 22,
          aihao: '篮球'
        }, {
          className: '二班',
          name: '李四6',
          sex: '男',
          age: 22,
          aihao: '足球'
        },

      ]

 

posted @ 2016-09-27 13:50  btg.yoyo  阅读(7288)  评论(1编辑  收藏  举报