js对列表里面的日期排序

1.时间正序:

            tem_list.sort(function(a,b){
              return Date.parse(a.ctime) - Date.parse(b.ctime); // 时间正序
            });

2.时间倒序:

            tem_list.sort(function(a,b){
              return Date.parse(b.ctime) - Date.parse(a.ctime); // 时间倒序
            });

3.解释:tem_list是数组,ctime是字典里面的时间key值,结构如下:

[
    {
        "ctime":"2020/6/25 7:2:37",
        "name":"Keepalived-20200625070237.zip",
        "dir":"/opt/log/keepalived/Keepalived/"
    },
    {
        "ctime":"2020/5/22 2:34:24",
        "name":"Keepalived-20200522023424.zip",
        "dir":"/opt/log/keepalived/Keepalived/"
    },
    {
        "ctime":"2020/5/26 10:12:15",
        "name":"Keepalived-20200526101215.zip",
        "dir":"/opt/log/keepalived/Keepalived/"
    },
    {
        "ctime":"2020/6/9 3:24:48",
        "name":"Keepalived-20200609032448.zip",
        "dir":"/opt/log/keepalived/Keepalived/"
    },
    {
        "ctime":"2020/6/22 10:22:6",
        "name":"Keepalived-20200622102206.zip",
        "dir":"/opt/log/keepalived/Keepalived/"
    }
]

返回值就是tem_list本身,返回排序后的新列表。
JavaScript sort() 方法

你可能感兴趣的:(前端)