10

I have the following object array where the key is the date in UTC format.

    Array = [{1436796000000:["Task1","Task2"],
         1437400800000:["Task4","Task8"],
         1436968800000: ["Task3","Task2"],
         1436882400000:["Task5","Task6"]}]

I want to sort this array object by key in descending order. So the expected output will be following like the latest date will come first.

    Array = [{1437400800000:["Task4","Task8"],
             1436968800000: ["Task3","Task2"],
             1436882400000:["Task5","Task6"],
             1436796000000:["Task1","Task2"]}]

How can I do this in javascript or using underscore.js?

fyasir
  • 2,924
  • 2
  • 23
  • 36
  • Impossible. No really. Arrays keys are always ordered ascendingly. Just iterate it backwards! – Bergi Jul 22 '15 at 03:12

3 Answers3

24

No, that isn't an array, it's an object, and Javascript objects' properties are unordered by definition; so sorting them is meaningless.

You could instead use an array, which does have order, and restructure your data like this:

var arr = [
  { date: 1436796000000, value: ["Task1","Task2"] },
  { date: 1437400800000, value: ["Task4","Task8"] },
  { date: 1436968800000, value: ["Task3","Task2"] },
  { date: 1436882400000, value: ["Task5","Task6"] }
]

and then you can sort it by date:

arr.sort( function ( a, b ) { return b.date - a.date; } );

If you don't want to restructure your data, you can iterate through it in the order you want, by getting an array of its keys and sorting that array, then using that array to access your object's properties, but you will need to do this each time your want to iterate through it in a particular order, since there is still no order information stored in the object:

// Get the array of keys
var keys = Object.keys( obj );

// Sort the keys in descending order
keys.sort( function ( a, b ) { return b - a; } );

// Iterate through the array of keys and access the corresponding object properties
for ( var i = 0; i < keys.length; i++ ) {
    console.log( keys[i], obj[ keys[i] ] );
}

You will need to shim Object.keys to support IE 8 and older browsers.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • I fear it actually is an array used as an object. – Bergi Jul 22 '15 at 03:13
  • @NutBoltu, No, after the edit you have an array with only one element (so it is always the first element in the array), that element is an object and its properties are unordered. – Paul Jul 22 '15 at 03:18
  • is there any way to order the object element? – fyasir Jul 22 '15 at 03:19
  • 2
    @NutBoltu No, there is no way. The definition of a Javascript object is `an **unordered** collection of properties`. You can however get an array of the object's keys, sort that and then iterate through it like so: `var keys = Object.keys( obj ); keys.sort( function ( a, b ) { return a - b; } ); for ( var i = 0; i < keys.length; i++ ) { console.log( keys[i], obj[ keys[i] ] ); }` – Paul Jul 22 '15 at 03:22
  • If you want to support IE 8 or older browsers you'll need to SHIM [Object.keys](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#Polyfill) for that to work: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#Polyfill/ – Paul Jul 22 '15 at 03:23
  • Thanks .Can you please update your answer with these information so that I can accept it. – fyasir Jul 22 '15 at 03:25
0

In Paulpro answer, I edit javascript array sort function (easy to understand):

function compare(a,b) {
   if (a.date < b.date )
     return -1;
   if (a.date  > b.date )
    return 1;
   return 0;
}
arr.sort(compare);

Here is my example: enter link description here

Here is relative post: enter link description here

Community
  • 1
  • 1
Giau Huynh
  • 300
  • 3
  • 15
0

Here is what I did for my use case hope this helps

var list = {30: "103", 40: "75", 50: "116", 100: "15"};
// Reverse sorting on key
const keysSorted = Object.keys(list).sort(function(a,b){return b-a})
console.log(keysSorted);

const arr = [];
// Adding the sorted result to an array of object
for (let i=0; i<keysSorted.length;i++) {
  const obj = {};
  obj.per= keysSorted[i];
  obj.val= list[keysSorted[i]];
  arr.push(obj);
}

console.log(arr);
nipun
  • 103
  • 1
  • 11