-3

Please help me to sort dates in ascending order with their value in JavaScript.

var arr = [{
  "3/10/2017": 52,
  "3/11/2017": 58,
  "3/12/2017": 70,
  "3/13/2017": 76,
  "3/14/2017": 88,
  "3/15/2017": 105,
  "3/16/2017": 113,
  "3/17/2017": 115,
  "3/18/2017": 125,
  "3/19/2017": 136,
  "3/20/2017": 139,
  "3/22/2017": 8,
  "3/23/2017": 13,
  "3/24/2017": 29,
  "3/25/2017": 36,
  "3/26/2017": 38,
  "3/27/2017": 56,
  "3/28/2017": 58,
  "3/29/2017": 68,
  "3/7/2017": 8,
  "3/8/2017": 21,
  "3/9/2017": 35
}]
karel
  • 3,880
  • 31
  • 37
  • 42
Arun Kumar M
  • 1,607
  • 1
  • 12
  • 24

1 Answers1

0

Objects can't be sorted in javascript, but you could rewrite your code as follows:

var arr = [
  ["3/10/2017", 52],
  ["3/11/2017", 58],
  ["3/12/2017", 70],
  ["3/13/2017", 76],
  ["3/14/2017", 88],
  ["3/15/2017", 105],
  ["3/16/2017", 113],
  ["3/17/2017", 115],
  ["3/18/2017", 125],
  ["3/19/2017", 136],
  ["3/20/2017", 139],
  ["3/22/2017", 8],
  ["3/23/2017", 13],
  ["3/24/2017", 29],
  ["3/25/2017", 36],
  ["3/26/2017", 38],
  ["3/27/2017", 56],
  ["3/28/2017", 58],
  ["3/29/2017", 68],
  ["3/7/2017", 8],
  ["3/8/2017", 21],
  ["3/9/2017", 35]
]

arr.sort(function(a,b) {
  if (a[1] < b[1]) return -1;
  else if (a[1] > b[1]) return 1;
  return 0;
});
Jan Tchärmän
  • 849
  • 1
  • 8
  • 11