I have an object in javascript
Object {HIDDEN ID: "06/03/2014", HIDDEN ID: "21/01/2014"}
I want to be able to create a new object that will have it sorted by recent date.
Like so:
SortedObject {"HIDDEN ID" : "21/01/2014", "HIDDEN ID" : "06/03/2014"}
I know how I could achieve this with an array but I dont know how to iterate through objects to sort by date.
Any help is much appreciated
EDIT:
Now I Have this code.
for(var x=0; x<folderList.length; x++){
retrieveAllFilesInFolder(folderList[x], function(){
var arr = [];
for(var i in fileListObj) {
var d = fileListObj[i].split("/");
arr.push({"id": i, "date":(new Date(d[2],d[1]-1,d[0]))});
}
arr.sort(function(a,b) { return a.date < b.date;});
console.log(arr);
});
}
However my output is not sorted bydate.
Object 1 id: "hiddenID1", date: Mon Nov 18 2013 00:00:00 GMT+0000 (GMT Standard Time)
Object 2 id: "hiddenId2", date: Thu Mar 06 2014 00:00:00 GMT+0000 (GMT Standard Time)
Object 3 id: "hiddenId3", date: Thu Sep 05 2013 00:00:00 GMT+0100 (GMT Daylight Time)
I have an object in javascript
Object {HIDDEN ID: "06/03/2014", HIDDEN ID: "21/01/2014"}
I want to be able to create a new object that will have it sorted by recent date.
Like so:
SortedObject {"HIDDEN ID" : "21/01/2014", "HIDDEN ID" : "06/03/2014"}
I know how I could achieve this with an array but I dont know how to iterate through objects to sort by date.
Any help is much appreciated
EDIT:
Now I Have this code.
for(var x=0; x<folderList.length; x++){
retrieveAllFilesInFolder(folderList[x], function(){
var arr = [];
for(var i in fileListObj) {
var d = fileListObj[i].split("/");
arr.push({"id": i, "date":(new Date(d[2],d[1]-1,d[0]))});
}
arr.sort(function(a,b) { return a.date < b.date;});
console.log(arr);
});
}
However my output is not sorted bydate.
Object 1 id: "hiddenID1", date: Mon Nov 18 2013 00:00:00 GMT+0000 (GMT Standard Time)
Object 2 id: "hiddenId2", date: Thu Mar 06 2014 00:00:00 GMT+0000 (GMT Standard Time)
Object 3 id: "hiddenId3", date: Thu Sep 05 2013 00:00:00 GMT+0100 (GMT Daylight Time)
Share
Improve this question
edited Apr 1, 2014 at 10:54
Joe
asked Apr 1, 2014 at 9:42
JoeJoe
2751 gold badge6 silver badges17 bronze badges
3
- look at stackoverflow./questions/3859239/sort-json-by-date – Tuhin Commented Apr 1, 2014 at 9:46
- JSON is a serialization format; "JSON objects" are strings. What you have are normal objects. – RemcoGerlich Commented Apr 1, 2014 at 10:28
- You can find some useful answers to this topic here: Sort Javascript Object Array By Date – jherax Commented Nov 5, 2014 at 14:10
2 Answers
Reset to default 4You cant sort an object as it contains key value pairs & not serialized items.
You must first construct an array & then sort it.
var obj = {"a" : "21/01/2014", "b" : "06/03/2014"};
var arr = [];
for(var i in obj) {
var d = obj[i].split("/");
arr.push({"id": i, "date":(new Date(d[2],d[1]-1,d[0]))});
}
arr.sort(function(a,b) { return a.date.valueOf() > b.date.valueOf();});
The result will be a an array arr
sorted by date.
you can't "sort" JSON objects; the only thing you could do would be to get the values into an array and sort the array. i assume the two "hidden id"'s are two distinct fields because your example is not valid JSON.