javascript - unable to create a sorted array based on creation date of s3 buckets -
i trying sort array of s3 buckets based on creation date (lastest first). getting same array repeated multiple times in sorted array. know missing here unable figure out what. code is:
var bucketlistarr = []; (var index in data.buckets) { var bucket = data.buckets[index]; //if(bucketlist[index] == null) bucketlist[index] = []; bucketlistarr.push({ "bucketname": bucket.name, "creationdate": bucket.creationdate }); // bucketlist[index][0] = bucket.name; // bucketlist[index][1] = bucket.creationdate; } var sortedbucketlistarr = []; //console.log("bucket list :: ", bucketlist, " length :: " , bucketlist.length ); for(var i= 0;i < bucketlistarr.length; i++){ for(var j = + 1; j< bucketlistarr.length; j++){ if(bucketlistarr[i].creationdate >= bucketlistarr[j].creationdate){ sortedbucketlistarr.push({ "bucketname": bucketlistarr[i].bucketname, "creationdate": bucketlistarr[i].creationdate }); } } } console.log("sorted list :: ", sortedbucketlistarr); thanks in advance help.
i able solve problem. problem date stored string. needs converted date before array can sorted on date property. altered code to:
var bucketlistarr = []; (var index in data.buckets) { var bucket = data.buckets[index]; //if(bucketlist[index] == null) bucketlist[index] = []; bucketlistarr.push({ "bucketname": bucket.name, "creationdate": bucket.creationdate }); } bucketlistarr.sort(function (a, b) { var datea = a.creationdate, dateb = b.creationdate; return dateb - datea;//a.strparamtosorton < b.strparamtosorton; }); console.log("sorted list :: ", bucketlistarr);
Comments
Post a Comment