zoukankan      html  css  js  c++  java
  • json对象按时间排序

    //正序
    var data = {
    "rows": [{
    "name": "张三",
    "time": "2011/4/1 0:00:00",
    },{
    "name": "李四",
    "time": "2015/5/6 12:30:00",
    },{
    "name": "王五",
    "time": "2012/10/1 22:10:00",
    },{
    "name": "赵六",
    "time": "2011/9/1 22:10:00",
    }]
    };
    var rows = data.rows;
    rows.sort(function(a,b){
    return Date.parse(a.time) - Date.parse(b.time);//时间正序
    });
    for(var i =0,l=rows.length;i<l;i++){
    console.log(rows[i].name + " | " + rows[i].time);
    }
    //倒序
    var data = {
    "rows": [{
    "name": "张三",
    "time": "2011/4/1 0:00:00",
    },{
    "name": "李四",
    "time": "2015/5/6 12:30:00",
    },{
    "name": "王五",
    "time": "2012/10/1 22:10:00",
    },{
    "name": "赵六",
    "time": "2011/9/1 22:10:00",
    }]
    };
    var rows = data.rows;
    rows.sort(function(a,b){
    return Date.parse(b.time)-Date.parse(a.time);//时间正序
    });
    for(var i =0,l=rows.length;i<l;i++){
    console.log(rows[i].name + " | " + rows[i].time);
    }

  • 相关阅读:
    Redis开发与运维:SDS
    Redis开发与运维:数据迁移
    我的2019上半年
    C# 并发编程
    经典排序算法 — C# 版(上)
    图解 -- 树的汇总
    图解--队列、并发队列
    栈到CLR
    我们的数组
    算法复杂度
  • 原文地址:https://www.cnblogs.com/horanly/p/8480081.html
Copyright © 2011-2022 走看看