0

how to short my list time wise ascending/ descending order

i have array like this:

this.array= [
    {name: A, time: 10:00am},
    {name: b, time: 10:05am},
    {name: c, time: 10:02am},
    {name: e, time: 09:00am}
]

i want to show this array time wise acceding order like:

this.array= [
    {name: e, time: 09:00am},
    {name: A, time: 10:00am},
    {name: c, time: 10:02am},
    {name: b, time: 10:05am},
]

1 Answers1

0

The best way would be to use MomentJS:

array= [
  {'name': 'A', 'time': '10:00am'},
  {'name': 'b', 'time': '10:05am'},
  {'name': 'c', 'time': '10:02am'},
  {'name': 'e', 'time': '09:00am'}
]

sorted = array.sort(function(a, b) {
    aT = new moment(a.time, 'HH:mm:ss a');
    bT = new moment(b.time, 'HH:mm:ss a');
    return aT.isBefore(bT) ? -1 : bT.isBefore(aT) ? 1 : 0;
});

To get MomentJS do:

npm install --save moment

then use it:

import * as moment from 'moment';

See https://momentjs.com/docs/#/use-it/typescript/.

heroin
  • 2,148
  • 1
  • 23
  • 32