javascript - Sort JSON Object by date value - Stack Overflow

I have an object in javascriptObject {HIDDEN ID: "06032014", HIDDEN ID: "21012014&qu

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
Add a ment  | 

2 Answers 2

Reset to default 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.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745362246a4624413.html

相关推荐

  • javascript - Sort JSON Object by date value - Stack Overflow

    I have an object in javascriptObject {HIDDEN ID: "06032014", HIDDEN ID: "21012014&qu

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信