Skip to content
Advertisement

Sorting An Array Of Objects Based On Time And Date

I have an object with the following structure:

var jsonData = {
    "1":["Test Event 1","5","interview","08:30:00","2016-05-28","1"],
    "2":["Test 2","2","Lesser Important Items","08:30:00","2016-05-27","0"],
    "3":["Test Event 4","5","meeting","08:30:00","2016-06-12","1"],
    "4":["","0","Lesser Important Items","08:30:00","2016-06-12","0"],
    "5":["","0","Lesser Important Items","08:30:00","2016-06-12","0"],
    "6":["Test Event 3","1","interview","19:30:00","2016-05-29","1"]
 }

I wanted to sort this on the basis of the time and date, and this is what I tried:

jsonData.sort(function(a, b) {
    return a.time - b.time;
})

but this is returning an error stating:

jsonData.sort is not a function

Advertisement

Answer

The sort() method can only apply applied to array, the provided data is an object.

If you just want the sorted index array

var data = {
  "1": ["Test Event 1", "5", "interview", "08:30:00", "2016-05-28", "1"],
  "2": ["Test 2", "2", "Lesser Important Items", "08:30:00", "2016-05-27", "0"],
  "3": ["Test Event 4", "5", "meeting", "08:30:00", "2016-06-12", "1"],
  "4": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"],
  "5": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"],
  "6": ["Test Event 3", "1", "interview", "19:30:00", "2016-05-29", "1"]
};


var res = Object.keys(data) // get object keys array
  .sort(function(a, b) { // sort the key array based on the date and time
    // convert to date and get difference for sorting
    return new Date(data[a][4] + ' ' + data[a][3]) - new Date(data[b][4] + ' ' + data[b][3]);
  })

console.log(res)

Or if you want to convert it to a sorted array based on the time and date then do something like this,

var data = {
  "1": ["Test Event 1", "5", "interview", "08:30:00", "2016-05-28", "1"],
  "2": ["Test 2", "2", "Lesser Important Items", "08:30:00", "2016-05-27", "0"],
  "3": ["Test Event 4", "5", "meeting", "08:30:00", "2016-06-12", "1"],
  "4": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"],
  "5": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"],
  "6": ["Test Event 3", "1", "interview", "19:30:00", "2016-05-29", "1"]
};


var res = Object.keys(data) // get object keys array
  .sort(function(a, b) { // sort the key array based on the date and time
    // convert to date and get difference for sorting
    return new Date(data[a][4] + ' ' + data[a][3]) - new Date(data[b][4] + ' ' + data[b][3]);
  }).map(function(v) { // use map to generate the object array
    return data[v] // get object from the data
  });

console.log(res)

In case if you want to re-index based the object keys then do something like this using sorted object array

var data = {
  "1": ["Test Event 1", "5", "interview", "08:30:00", "2016-05-28", "1"],
  "2": ["Test 2", "2", "Lesser Important Items", "08:30:00", "2016-05-27", "0"],
  "3": ["Test Event 4", "5", "meeting", "08:30:00", "2016-06-12", "1"],
  "4": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"],
  "5": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"],
  "6": ["Test Event 3", "1", "interview", "19:30:00", "2016-05-29", "1"]
};


Object.keys(data) // get object keys array
  .sort(function(a, b) { // sort the key array based on the date and time
    // convert to date and get difference for sorting
    return new Date(data[a][4] + ' ' + data[a][3]) - new Date(data[b][4] + ' ' + data[b][3]);
  }).map(function(v) { // use map to generate the object array
    return data[v] // get object from the data
  }).forEach(function(v, i) {
    data[i + 1] = v; // update based on the sorted array
  })

console.log(data)
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement