Hi,
I know this must be relatively simple, but I have a dataset of JSON that I would like to sort by date. So far, I've run into problems at every turn.
Right now I have the date stored as this.lastUpdated
.
I have access to jquery if that helps, but I realize the .sort() is native JS.
Thanks in advance.
views:
82answers:
1
A:
Assuming that you have an array of javascript objects, just use a custom sort function:
function custom_sort(a, b) {
return new Date(a.lastUpdated).getTime() - new Date(b.lastUpdated).getTime();
}
var your_array = [
{lastUpdated: "2010/01/01"},
{lastUpdated: "2009/01/01"},
{lastUpdated: "2010/07/01"}
];
your_array.sort(custom_sort);
Sean Vieira
2010-10-04 21:31:45
Unserialized JSON is a non-array object. `.sort()` is only available on arrays.
Peter Ajtai
2010-10-05 00:40:42