1

I want to sort the date and time while pushing the items to an array. So far, I can sort just the date but not able to sort the time. If the same date has two times, then how do I sort the date first and then the timing for that date.

let items = [
{'id': 'id7', 'date': '2020-08-17', 'time': '05:02:54.000'},
{'id': 'id2', 'date': '2020-05-12', 'time': '02:09:57.000'}, 
{'id': 'id3', 'date': '2020-05-12', 'time': '03:04:44.000'}, //both the 2020-05-12 date has two timings so I want to sort these time too along with the date
{'id': 'id4', 'date': '2020-09-23', 'time': '07:16:23.000'}
];

const headers = [
'ID',
'Date',
'Time'
];

dataArr = []

items.forEach(item => {
    dataArr.push([
        item.id,
        item.date,
        item.time
    ]);
})
;
let sortedData = [headers, ...dataArr.sort((a, b) => a[1].localeCompare(b[1]))];

final sorted array must look something like


['id': 'id2', 'date': '2020-05-12', 'time': '02:09:57.000'], 
['id': 'id3', 'date': '2020-05-12', 'time': '03:04:44.000'],
['id': 'id7', 'date': '2020-08-17', 'time': '05:02:54.000'],
['id': 'id4', 'date': '2020-09-23', 'time': '07:16:23.000']

3 Answers3

0

try to sort both date and time in a single sort function

let ss = [
{'id': 'id7', 'date': '2020-08-17', 'time': '05:02:54.000'},
{'id': 'id2', 'date': '2020-05-12', 'time': '02:09:57.000'}, 
{'id': 'id3', 'date': '2020-05-12', 'time': '03:04:44.000'}, 
{'id': 'id4', 'date': '2020-09-23', 'time': '07:16:23.000'}
];

let dd = ss.sort((a,b)=>new Date(a.date+':'+a.time) - new Date(b.date+':'+b.time))

console.log(dd)
Snivio
  • 1,741
  • 1
  • 17
  • 25
  • for some reason, in my code, it's showing in descending order for the time, although the date is looking fine. I tried it ```let sortedData = [headers, ...dataArr.sort((a,b)=>new Date(a[1]+':'+a[2]) - new Date(b[1]+':'+b[2]))];``` – allhopelost Oct 08 '20 at 12:54
0
let sortedData = [headers, ...dataArr.sort((a, b) => a[1].localeCompare(b[1])==0 ? a[2].localeCompare(b[2]) : a[1].localeCompare(b[1]))];

let items = [
{'id': 'id7', 'date': '2020-08-17', 'time': '05:02:54.000'}, 
{'id': 'id3', 'date': '2020-05-12', 'time': '03:04:44.000'},
{'id': 'id4', 'date': '2020-09-23', 'time': '07:16:23.000'},
{'id': 'id2', 'date': '2020-05-12', 'time': '02:09:57.000'}
];

const headers = [
'ID',
'Date',
'Time'
];

dataArr = []

items.forEach(item => {
    dataArr.push([
        item.id,
        item.date,
        item.time
    ]);
})
;
let sortedData = [headers, ...dataArr.sort((a, b) => a[1].localeCompare(b[1])==0 ? a[2].localeCompare(b[2]) : a[1].localeCompare(b[1]))];
console.log(sortedData)
twothreezarsix
  • 375
  • 3
  • 13
0

You can try something like this:

function compare (a,b) {
    if(a<b) return -1;
    else if(a>b) return 1;
    else return 0;
}

items.sort((a,b) => {
    let d = compare(a.date, b.date);
    if(d < 0) return -1;
    else if(d > 0) return 1;
    else {
         //Same date compare time
        let t = compare(a.time, b.time);
        if(t < 0) return -1;
        else if(t > 0) return 1;
        else return 0;


    }

});



This compares the date first. If dates differ, you don't need to compare time. If dates are same, you can compare time.

Abrar Hossain
  • 2,594
  • 8
  • 29
  • 54