JSON 通过两个字段来排序

标签: json排序 json两个字段排序
4人阅读 评论(0) 收藏 举报
分类:
 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;
            });
查看评论

js对Json数据按照某一字段排序的方法

js对Json数据按照某一字段排序的方法 function JsonSort() { //测试Json数据var obj = [{ a: change2Date("2012-1-7"), b: 55...
  • qq_27399407
  • qq_27399407
  • 2017-04-28 11:57:53
  • 560

基于JavaScript实现Json数据根据某个字段(json中的某个属性)进行排序

在实际开发中,有一种需求是当前台获取后台传过来的json数据时,比如,需要按照json数据中查询的某个属性(某个字段)进行排序,好比,我需要显示某个事项要按照第1阶段、第2阶段等等以此类推的显示。我的...
  • wdhhxbb
  • wdhhxbb
  • 2017-06-01 16:47:25
  • 1606

mysql 查询根据两个或两个以上字段排序

以jibingName=腹痛 字段为例                  -------------------------------------------------------------...
  • u012503756
  • u012503756
  • 2017-01-22 09:38:47
  • 3422

JSON对象字符串数组多字段(多列)排序

JSON对象字符串数组多字段(多列)排序 /* * 功能:对JSON对象字符串数组进行多字段(多列)排序 * 参数: * objArr: 目标数组 * keyArr: 排序字段,以数组形...
  • u013992330
  • u013992330
  • 2017-12-19 18:25:01
  • 176

jq按json数据某一字段进行排序

导航条吸附效果,并随着页面滚动选中相应导航内容 .content-box{ border: 1px solid red; ...
  • u012076852
  • u012076852
  • 2016-07-20 17:27:34
  • 2567

JSON 数字排序 多字段排序

原文http://bbs.csdn.net/topics/390594744?page=1#post-395599672 //排序数组 function SortBy(field, reverse...
  • qq873113580
  • qq873113580
  • 2013-09-18 15:15:56
  • 1145

对XML实现自定义、多字段排序

对Xml排序,除了使用XSLT之外,还可以使用XPathExpression类,但这些类一般情况下只支持数字和字符串类型的数据类型排序。要实现按照自己的排序方法排序,需要自己进行定义。AddSort ...
  • net_lover
  • net_lover
  • 2007-09-13 17:17:00
  • 6060

thinkphp 多字段排序

order("id desc,title desc,onclick desc")
  • lengyue1084
  • lengyue1084
  • 2013-07-03 15:16:08
  • 1327

mysql字段存储json结构数据,按照某个属性排序

SELECT *  FROM  tb_course WHERE category_id LIKE '%\"13\"%' ORDER BY SUBSTRING(sn, LOCATE('\"13-',sn...
  • shxt_xiaozhi
  • shxt_xiaozhi
  • 2016-11-21 13:17:33
  • 1794

order by两个字段双重排序

order by cloume1 asc,cloume2 desc
  • pg_Roger
  • pg_Roger
  • 2015-07-14 16:03:33
  • 2461
    个人资料
    持之以恒
    等级:
    访问量: 1万+
    积分: 345
    排名: 23万+
    文章分类
    最新评论