根据日期和时间对JSON数据进行排序 [英] Sort JSON data based on Date and Time
本文介绍了根据日期和时间对JSON数据进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用jQuery/Javascript根据时间(2016-12-07T13:00:00)重新排列以下JSON格式.
Is it possible to rearrange the below JSON format based on time (2016-12-07T13:00:00) using jQuery/Javascript.
[
{
"id":1,
"start":"2016-12-07T13:00:00",
"subject":"test1",
},
{
"id":2,
"start":"2016-12-07T09:00:00",
"subject":"test2",
},
{
"id":3,
"start":"2016-12-07T10:00:00",
"subject":"test3",
},
{
"id":4,
"start":"2016-12-07T07:00:00",
"subject":"test4",
},
{
"id":5,
"start":"2016-12-07T14:00:00",
"subject":"test5",
}
]
推荐答案
您可以使用 ISO 8601 日期可作为字符串排序.
You could use String#localeCompare
in a sort callback for the property start
, because ISO 8601 dates are sortable as string.
var array = [
{ id: 1, start: "2016-12-07T13:00:00", subject: "test1" },
{ id: 2, start: "2016-12-07T09:00:00", subject: "test2" },
{ id: 3, start: "2016-12-07T10:00:00", subject: "test3" },
{ id: 4, start: "2016-12-07T07:00:00", subject: "test4" },
{ id: 5, start: "2016-12-07T14:00:00", subject: "test5" }
];
array.sort(function (a, b) {
return a.start.localeCompare(b.start);
});
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
这篇关于根据日期和时间对JSON数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文