Below is my json data:
以下是我的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"
}
]
};
I want to sort the array according to DutyStart
(in date format) in Descending and Ascending order using Javascript only and append the data to body or alert the sorted array.
我想根据DutyStart(日期格式)使用Javascript按降序和升序排序数组,并将数据附加到正文或警告已排序的数组。
I tried the solution fromThis question but iam not able to get it.
我试过这个问题的解决方案,但我无法得到它。
Thanks in advance.
提前致谢。
3 个解决方案
#1
1
You can sort very easily on ISO 8601-like date strings since they work as strings as well as numbers would.
您可以非常轻松地对类似ISO 8601的日期字符串进行排序,因为它们可以像字符串一样工作。
Pass the values to a sort function and return 0, 1 or -1 depending the comparison, e.g.
将值传递给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));
#2
1
Simple bubble sort for your data, sorting by the date:
您的数据的简单冒泡排序,按日期排序:
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);
Hope this helps.
希望这可以帮助。
#3
0
I suggest to use String#localeCompare
for this task, because it compares strings and while the data are ISO 8601 date strings, they can be sorted directly with this method.
我建议使用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>');
#1
1
You can sort very easily on ISO 8601-like date strings since they work as strings as well as numbers would.
您可以非常轻松地对类似ISO 8601的日期字符串进行排序,因为它们可以像字符串一样工作。
Pass the values to a sort function and return 0, 1 or -1 depending the comparison, e.g.
将值传递给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));
#2
1
Simple bubble sort for your data, sorting by the date:
您的数据的简单冒泡排序,按日期排序:
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);
Hope this helps.
希望这可以帮助。
#3
0
I suggest to use String#localeCompare
for this task, because it compares strings and while the data are ISO 8601 date strings, they can be sorted directly with this method.
我建议使用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>');