-2

How to sort array by num property in this JavaScript array.

var data = [{
    005: { `num`: 1360487},
    047: { `num`: 2519472},
    061: { `num`: 1559115},
    081: { `num`: 2232710},
    085: { `num`: 54956 }
  }];

enter image description here

Andreas
  • 21,535
  • 7
  • 47
  • 56
Vishal Bedre
  • 119
  • 1
  • 8
  • @Andreas: Thanks, you're right. I reopened it. Here's a better dupe: http://stackoverflow.com/questions/1069666/sorting-javascript-object-by-property-value –  Aug 08 '16 at 21:33
  • You want to sort by property value? – Robert Aug 08 '16 at 21:36

1 Answers1

0

What you are manipulating is an array containing 1 object, not an array of objects. You should probably change the structure of data to make it easier to manipulate, an example would be:

var data = [
    [005, { num: 1360487}],
    [047, { num: 2519472}],
    [061, { num: 1559115}],
    [081, { num: 2232710}],
    [085, { num: 54956 }],
];

data.sort(
    function (firstElem, secondElem) {
        return firstElem[1].num - secondElem[1].num;
  }
);

// data is sorted
Théo
  • 655
  • 7
  • 17
  • yeah the result is not logged so unless you print it you won't see anything... ran it in your browser console and check the result. – Théo Aug 08 '16 at 21:52