weixin_33671935 2014-06-09 15:40 采纳率: 0%
浏览 35

按值排序json对象

I have a json object like below, how do I sort it using date?

json = {"date_hash":{"second_bleed":"2014-09-08","sixth_boost":"2014-10-28","first_boost":"2014-06-24","first_bleed":"2014-08-08","fifth_boost":"2014-09-30","fourth_bleed":"2014-11-03","second_boost":"2014-07-15","fourth_boost":"2014-09-02","third_bleed":"2014-10-06","primary_injection":"2014-06-02","third_boost":"2014-08-05"}}

I tried doing

json['date_hash'].sort(function(a, b){

});

Sort is not a function for json?

  • 写回答

1条回答 默认 最新

  • weixin_33704591 2014-06-09 15:51
    关注

    The date_hash object is a JSON object which doesn't have an order (not sortable).

    You should try to use an array:

    {"date_hash": [
        {"name": "second_bleed", "date": "2014-09-08"},
       ....
       {"name": "sixth_boost", "date": "2014-09-28"}
    ]}
    

    and then you something similar to the function you're using to sort.

    function (a, b) {
        if (a.date < b.date) {
            return -1;
        } else if (a.date > b.date) {
            return 1;
        };
        return 0;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵