Array Sort by time hh:mm:ss

前端 未结 3 1080
孤独总比滥情好
孤独总比滥情好 2021-01-07 15:36

I am trying to sort the time. but I am unable to sort by time (hh:mm:ss) format. so i have used moments js. my array sort by time not get sorted. how sort array by using map

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-07 16:30

    You can lexicographical sort the time using string.localeCompare().

    let times = [ { "id": 1, "date": "02:01:02" }, { "id": 2, "date": "01:01:01" }, { "id": 3, "date": "03:01:01" }, { "id": 4, "date": "04:01:01" } ];
    times.sort((a,b) => a.date.localeCompare(b.date));
    console.log(times);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

提交回复
热议问题