I have an object with the following structure:
JavaScript
x
9
1
var jsonData = {
2
"1":["Test Event 1","5","interview","08:30:00","2016-05-28","1"],
3
"2":["Test 2","2","Lesser Important Items","08:30:00","2016-05-27","0"],
4
"3":["Test Event 4","5","meeting","08:30:00","2016-06-12","1"],
5
"4":["","0","Lesser Important Items","08:30:00","2016-06-12","0"],
6
"5":["","0","Lesser Important Items","08:30:00","2016-06-12","0"],
7
"6":["Test Event 3","1","interview","19:30:00","2016-05-29","1"]
8
}
9
I wanted to sort this on the basis of the time and date, and this is what I tried:
JavaScript
1
4
1
jsonData.sort(function(a, b) {
2
return a.time - b.time;
3
})
4
but this is returning an error stating:
JavaScript
1
2
1
jsonData.sort is not a function
2
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
JavaScript
1
17
17
1
var data = {
2
"1": ["Test Event 1", "5", "interview", "08:30:00", "2016-05-28", "1"],
3
"2": ["Test 2", "2", "Lesser Important Items", "08:30:00", "2016-05-27", "0"],
4
"3": ["Test Event 4", "5", "meeting", "08:30:00", "2016-06-12", "1"],
5
"4": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"],
6
"5": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"],
7
"6": ["Test Event 3", "1", "interview", "19:30:00", "2016-05-29", "1"]
8
};
9
10
11
var res = Object.keys(data) // get object keys array
12
.sort(function(a, b) { // sort the key array based on the date and time
13
// convert to date and get difference for sorting
14
return new Date(data[a][4] + ' ' + data[a][3]) - new Date(data[b][4] + ' ' + data[b][3]);
15
})
16
17
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,
JavaScript
1
19
19
1
var data = {
2
"1": ["Test Event 1", "5", "interview", "08:30:00", "2016-05-28", "1"],
3
"2": ["Test 2", "2", "Lesser Important Items", "08:30:00", "2016-05-27", "0"],
4
"3": ["Test Event 4", "5", "meeting", "08:30:00", "2016-06-12", "1"],
5
"4": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"],
6
"5": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"],
7
"6": ["Test Event 3", "1", "interview", "19:30:00", "2016-05-29", "1"]
8
};
9
10
11
var res = Object.keys(data) // get object keys array
12
.sort(function(a, b) { // sort the key array based on the date and time
13
// convert to date and get difference for sorting
14
return new Date(data[a][4] + ' ' + data[a][3]) - new Date(data[b][4] + ' ' + data[b][3]);
15
}).map(function(v) { // use map to generate the object array
16
return data[v] // get object from the data
17
});
18
19
console.log(res)
In case if you want to re-index based the object keys then do something like this using sorted object array
JavaScript
1
21
21
1
var data = {
2
"1": ["Test Event 1", "5", "interview", "08:30:00", "2016-05-28", "1"],
3
"2": ["Test 2", "2", "Lesser Important Items", "08:30:00", "2016-05-27", "0"],
4
"3": ["Test Event 4", "5", "meeting", "08:30:00", "2016-06-12", "1"],
5
"4": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"],
6
"5": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"],
7
"6": ["Test Event 3", "1", "interview", "19:30:00", "2016-05-29", "1"]
8
};
9
10
11
Object.keys(data) // get object keys array
12
.sort(function(a, b) { // sort the key array based on the date and time
13
// convert to date and get difference for sorting
14
return new Date(data[a][4] + ' ' + data[a][3]) - new Date(data[b][4] + ' ' + data[b][3]);
15
}).map(function(v) { // use map to generate the object array
16
return data[v] // get object from the data
17
}).forEach(function(v, i) {
18
data[i + 1] = v; // update based on the sorted array
19
})
20
21
console.log(data)