【问题标题】:Javascript - Sort nested array containing objects ascending and descending according to dateJavascript - 根据日期对包含对象升序和降序的嵌套数组进行排序
【发布时间】:2016-05-21 20:00:51
【问题描述】:

下面是我的json数据:

var homes={
"ERROR": "SUCCESS",
"DATA": [
    {
        "BookingID": "9513",
        "DutyStart": "2016-02-11 12:00:00"
    },
    {
        "BookingID": "91157307",
        "DutyStart": "2016-02-11 13:00:00"
    },
    {
        "BookingID": "95117317",
        "DutyStart": "2016-02-11 13:30:00"
    },
    {
        "BookingID": "957266",
        "DutyStart": "2016-02-12 19:15:00"
    },
    {
        "BookingID": "74",
        "DutyStart": "2016-02-11 12:21:00"
    }
]
};

我想仅使用 Javascript 根据DutyStart(日期格式)以降序和升序对数组进行排序,并将数据附加到正文或提醒排序后的数组。

我尝试了This question 的解决方案,但我无法得到它。

提前致谢。

【问题讨论】:

  • 你有没有尝试过?
  • 提示。您甚至可以在不将它们转换为日期的情况下比较这些日期字符串。你有没有尝试过?
  • @OgnjenBabic 我在stackoverflow.com/questions/16574309/… 尝试了解决方案,但问题是正在创建像[{...}] 这样的json,但我有{a:xxx, b:[{..}]}。因为我是新手,所以我无法得到它。

标签: javascript arrays json sorting


【解决方案1】:

对您的数据进行简单冒泡排序,按日期排序:

var homes={
"ERROR": "SUCCESS",
"DATA": [
    {
        "BookingID": "9513",
        "DutyStart": "2016-02-11 12:00:00"
    },
    {
        "BookingID": "91157307",
        "DutyStart": "2016-02-11 13:00:00"
    },
    {
        "BookingID": "95117317",
        "DutyStart": "2016-02-11 13:30:00"
    },
    {
        "BookingID": "957266",
        "DutyStart": "2016-02-12 19:15:00"
    },
    {
        "BookingID": "74",
        "DutyStart": "2016-02-11 12:21:00"
    }
]
};


var list = homes.DATA.sort(function(a, b) {
    return a.DutyStart - b.DutyStart;
});
console.log(list);

希望这会有所帮助。

【讨论】:

  • 为什么不像homes.DATA.sort(sortFn(a, b))这样的?
  • 谢谢@RobG。更新答案以反映
【解决方案2】:

您可以在类似 ISO 8601 的日期字符串上非常轻松地sort,因为它们的工作方式与数字一样。

将值传递给排序函数并根据比较返回 0、1 或 -1,例如

// Data
var homes={
"ERROR": "SUCCESS",
"DATA": [
    {
        "BookingID": "9513",
        "DutyStart": "2016-02-11 12:00:00"
    },
    {
        "BookingID": "91157307",
        "DutyStart": "2016-02-11 13:00:00"
    },
    {
        "BookingID": "95117317",
        "DutyStart": "2016-02-11 13:30:00"
    },
    {
        "BookingID": "957266",
        "DutyStart": "2016-02-12 19:15:00"
    },
    {
        "BookingID": "74",
        "DutyStart": "2016-02-11 12:21:00"
    }
]
};

// Sort - operates on the original array
homes.DATA.sort(function(a, b) {
  return a.DutyStart < b.DutyStart? -1 : (a.DutyStart == b.DutyStart? 0 : 1); 
});

document.write(JSON.stringify(homes.DATA));

【讨论】:

  • 谢谢@RobG。你能解释一下这是什么意思将值传递给排序函数并根据比较返回0、1或-1以及如何升序和降序进行排序。跨度>
  • 是对代码的解释。您可以通过颠倒比较结果来颠倒排序顺序,因此而不是 a &lt; b? -1 : (a == b? 0 : 1) 交换 -1 和 1:a &lt; b? 1 : (a == b? 0 : -1)。这里有很多关于排序数组的问题,搜索一下。
【解决方案3】:

我建议使用String#localeCompare来完成这个任务,因为它比较字符串,而数据是ISO 8601日期字符串,可以直接用这个方法排序。

var homes = { "ERROR": "SUCCESS", "DATA": [{ "BookingID": "9513", "DutyStart": "2016-02-11 12:00:00" }, { "BookingID": "91157307", "DutyStart": "2016-02-11 13:00:00" }, { "BookingID": "95117317", "DutyStart": "2016-02-11 13:30:00" }, { "BookingID": "957266", "DutyStart": "2016-02-12 19:15:00" }, { "BookingID": "74", "DutyStart": "2016-02-11 12:21:00" }] };

homes.DATA.sort(function (a, b) {
    return a.DutyStart.localeCompare(b.DutyStart);
});

document.write('<pre>' + JSON.stringify(homes, 0, 4) + '</pre>');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多