离子2时间分选

时间:2018-02-08 07:06:44

标签: sorting ionic2

如何缩短我的列表时间明智的升序/降序

我有这样的数组:

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

我想要显示此数组时间明智的加入顺序,如:

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

1 个答案:

答案 0 :(得分:0)

最好的方法是使用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;
});

要获得MomentJS:

npm install --save moment

然后使用它:

import * as moment from 'moment';

请参阅https://momentjs.com/docs/#/use-it/typescript/