0

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)
Joe
  • 267
  • 1
  • 6
  • 17
  • look at http://stackoverflow.com/questions/3859239/sort-json-by-date – Tuhin Apr 01 '14 at 09:46
  • JSON is a serialization format; "JSON objects" are strings. What you have are normal objects. – RemcoGerlich Apr 01 '14 at 10:28
  • You can find some useful answers to this topic here: **[Sort Javascript Object Array By Date](http://stackoverflow.com/a/26759127/2247494)** – jherax Nov 05 '14 at 14:10

2 Answers2

4

You 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.

loxxy
  • 12,990
  • 2
  • 25
  • 56
  • Thanks for your answer, I now have this running however it has not sorted them. I have done console.log(arr) and it shows as an array holding many objects each object has an id and a date. – Joe Apr 01 '14 at 10:38
  • Yes, and arr should be sorted. – loxxy Apr 01 '14 at 10:39
  • No it was not, I cant get it working right now haha it showed the array in the console before now it doesnt. – Joe Apr 01 '14 at 10:44
  • Object 1 date: Mon Nov 18 2013 00:00:00 GMT+0000 (GMT Standard Time) Object 2 date: Thu Mar 06 2014 00:00:00 GMT+0000 (GMT Standard Time) Object 3 date: Thu Sep 05 2013 00:00:00 GMT+0100 (GMT Daylight Time) This is not sorted – Joe Apr 01 '14 at 10:51
  • Does this work? `arr.sort(function(a,b) { return a.date.valueOf() > b.date.valueOf();});` – loxxy Apr 01 '14 at 11:03
  • This may have indeed worked. However sometimes I am running my code and it prints the whole list of objects, and sometimes it prints only 1 object and sometimes none at all. It always prints '[]' in the console though. Im a bit stumped. – Joe Apr 01 '14 at 11:41
  • Ok it has worked however it seems to be backwards, is there a way to reverse the order? :P, as Object 24 has the most recent date, I would like Object 0 to have the most recent instead. – Joe Apr 01 '14 at 11:43
  • change `>` to `<` in the sort function – loxxy Apr 01 '14 at 11:51
  • Thanks a lot, I am not sure why I am getting nearly 300 objects, It is repeating a ids/dates so many times, any reason why? – Joe Apr 01 '14 at 12:06
1

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.

marco
  • 806
  • 1
  • 7
  • 17
  • Yes they are, If I take the values out, how do I then match the date back with it the correct 'hidden id'? – Joe Apr 01 '14 at 09:49
  • I think it's best if you provide an example of what your result is expected to be. You can't sort objects so `{"a": 1, "b": 2}` and `{"b": 2, "a": 1}` are the same thing, if that's what you want you can't have it. – marco Apr 01 '14 at 09:51
  • My expected result would be to have a new object with the ids sorted by date. {"id5" : "02/01/2014", "id3" : "01/01/2014", "id1" : "05/01/2013"} – Joe Apr 01 '14 at 09:53
  • @Joe — As this answer says, you cannot sort JSON objects. They are, by definition, unordered. Arrays have order, objects do not. – Quentin Apr 01 '14 at 09:55
  • Ok then, it is not possible to put the dates into an array and sort them after, match the dates with their relevant ids? – Joe Apr 01 '14 at 09:56
  • Sure it is, but if you put them back into an object afterwards, the order will be lost. – RemcoGerlich Apr 01 '14 at 10:29