JSON 通过两个字段来排序

 var sorted = data.sort(function(a, b) {   //先排序a 字段   然后排序b 字段
                const first = a.a - b.a;
                if (first !== 0) {
                    return first;
                }

                return a.b - b.b;
            });

例子如下

   var data=[{"id":21793,"account_id":10000,"child_id":10028,"child_name":"crj","package":"10","amount":100},{"id":21841,"account_id":10000,"child_id":10060,"child_name":"客户2","package":"10","amount":200},{"id":21849,"account_id":10000,"child_id":10052,"child_name":"客户","package":"10","amount":1200},{"id":21853,"account_id":10000,"child_id":10056,"child_name":"客户1","package":"10","amount":118},{"id":21865,"account_id":10000,"child_id":10104,"child_name":"客户13","package":"10","amount":1},{"id":21869,"account_id":10000,"child_id":10116,"child_name":"客户14","package":"10","amount":1},{"id":21873,"account_id":10000,"child_id":10128,"child_name":"客户17","package":"10","amount":1},{"id":21877,"account_id":10000,"child_id":10132,"child_name":"客户18","package":"10","amount":1},{"id":21885,"account_id":10000,"child_id":10144,"child_name":"客户21","package":"10","amount":1},{"id":21889,"account_id":10000,"child_id":10148,"child_name":"客户22","package":"10","amount":1},{"id":21897,"account_id":10000,"child_id":10092,"child_name":"客户10","package":"10","amount":600}]
  var sorted = data.sort(function(child_id, package) {  
                const first = child_id.child_id - package.child_id;
                if (first !== 0) {
                    return first;
                }

                return child_id.package - package.package;
            });
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页