0

I have an object that looks like this

myList: {  
id1:{  
      ts:'2010-01-12T00:51:00',
      name:"roger"
     },  
id2:{  
      ts:'2011-01-12T05:22:00',
      name: "Tom"
  },  
id3:{  
      ts:'2013-01-12T11:32:00',
      name:"Jack"
  }
}

I know objects cant be sorted so i wanted to know how i can generate an array of just the keys,which are sorted according to the key "ts". I want this in descending order.

So the array for the above object will be [id3,id2,id1]

once i have this array i can make operations like this where arr is sorted array and myList is the object

for(var i=0:i<arr.length;i++)
{
alert(myList[arr[i]].name);
}
9
  • String comparison of dates? Commented Jan 20, 2014 at 7:12
  • As written, the ts values above are 1997, 1998 and 2000, but it seems doubtful that this is what's intended. Commented Jan 20, 2014 at 7:16
  • I am new to js so if you have a soln where i can generate such an array i would appreciate it Commented Jan 20, 2014 at 7:17
  • 1
    You may want to read stackoverflow.com/questions/2947822/javascript-sort-objects Also, string should have quotes around them or else it's just math at best. Commented Jan 20, 2014 at 7:20
  • 2
    You basically want a collection: list=[{id:1,ts:'2010-01-12'},{id:2,ts:'2011-01-12'},...] Commented Jan 20, 2014 at 7:22

3 Answers 3

1
var keys = Object.keys(myList).sort(function(a, b) {
    if (myList[a].ts == myList[b].ts) {
        return 0;
    }
    return myList[a].ts < myList[b].ts ? 1 : -1;
});

console.log(keys);

jsfiddle: http://jsfiddle.net/pWq2L/

Explanation:

  1. First you export keys from the object: Object.keys(myList)
  2. You sort using custom comparison function and in it you refer to the .ts attribute of the compared values. a and b are the keys of the compared elements

References:

Sign up to request clarification or add additional context in comments.

Comments

0

Given this data structure, and assuming there is a good reason for it to not be an array:

var myList = {
  id1: {  
    ts: new Date('2010-01-12T00:51:00'),
    name: 'Roger'
  },
  id2: {  
    ts: new Date('2011-01-12T05:22:00'),
    name: 'Tom'
  },  
  id3:{  
    ts: new Date('2013-01-12T11:32:00'),
    name: 'Jack'
  }
}

We can create and sort an array like so:

var arr = [];
Object.keys(myList).forEach(function(item) {
  arr.push(myList[item]);
});
arr.sort(function(a, b) {
  return a.ts > b.ts ? -1 : a.ts < b.ts ? 1 : 0;
})

Comments

0

One way to extend zerkms solution is to work with an object augmented with smart properties. The versatile reduce comes in handy:

var result = keys.reduce(function(arr, k, i) {
    var item = {
      key: k,
      index: i,
      value: myList[k]
    };
    arr.push(item);
    return arr;
}, []);


//result:
[
  { index:0 , key: id3, value: {...} },
  { index:1 , key: id2, value: {...} },
  { index:2 , key: id1, value: {...} }
]

reduce is part of the ECMAScript 5th edition; so you may fill in some gap with a polyfill.

http://jsfiddle.net/pWq2L/3/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.