Sorting JS Object

Hi all ,

var data = [
  {
    "metadata" : {
      "names":["times","values","types"],
      "types":["time","linear","ordinal"]
    },
    "data": [
      ["1459016130000",12.0,"GAP"],
      ["1459016130003",12.0,"GAP"],
      ["1459016130001",12.0,"GAP"],
      ["1459016130002",12.0,"GAP"]
    ]
  }
];

How can I sort this JSON by timestamp value ?

straight off MDN:

data[0].data.sort(function(a, b) {
    return a[0] - b[0];
});
1 Like

Hi @Mohit_Singh, do you mean that data[0].data array? That could be done like

data[0].data.sort((a, b) => a[0] - b[0]);

x-post

1 Like

This worked btw I used the way @Dormilich mentioned . Thank you so much @m3g4p0p and “=>” not supported in IDE.

This outputs undefined error.

Arrow functions are ES2015. They are not well supported.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.