首页 > 解决方案 > 如果JS中存在值,如何根据日期按降序对数组进行排序?

问题描述

我正在尝试根据降序对数据进行排序,publishDate但它不起作用。在某些阵列中,publishDate正在到来,在某些阵列中,它没有到来。

[
    {
        "id": "brexit-delay",
        "title": "Brexit Delay",
        "publish": {

            "publishDate": "2019-8-30T12:25:47.938Z",
        }
    },   

    {
        "id": "brexit-delay",
        "title": "Example 3"
    },    

    {
        "id": "brexit-delay",
        "title": "Example 2",
        "publish": {           
            "publishDate": "2019-6-30T12:25:47.938Z",
        }
    },

    {
        "id": "brexit-delay",
        "title": "Example 5"
    },    

    {
        "id": "brexit-delay",
        "title": "Example 5",
        "publish": {           
            "publishDate": "2019-10-25T12:25:47.938Z",
        }
    }
]   

js代码:

data.sort(function(a, b){
    if("publish" in a && "publish" in b){
        return new Date(a.publish.publishDate) - new Date(b.publish.publishDate)
    }
} );

标签: javascriptarraysnode.jssorting

解决方案


检查该属性是否存在并基于此退货单:

const myParseDate = date_string => {
    let [y,M,d,h,m,s] = date_string.split(/[- :T]/);
    return new Date(y,parseInt(M)-1,d,h,parseInt(m),s.replace('Z',''));
}

arr.sort(function(a, b){
    if (a.publish  && b.publish) {
        return myParseDate(b.publish.publishDate) - myParseDate(a.publish.publishDate)
    }
    else if (a.hasOwnProperty("publish"))
        return -1;
    else if (b.hasOwnProperty("publish"))
        return 1;
    else
        return 0;
} );

一个例子:

const arr = [
{
    "id": "brexit-delay",
    "title": "Brexit Delay",
    "publish": {
        "publishDate": "2019-8-30T12:25:47.938Z",
    }
},
{
    "id": "brexit-delay",
    "title": "Example 3"
},
{
    "id": "brexit-delay",
    "title": "Example 2",
    "publish": {
        "publishDate": "2019-6-30T12:25:47.938Z",
    }
},
{
    "id": "brexit-delay",
    "title": "Example 5"
},
{
    "id": "brexit-delay",
    "title": "Example 5",
    "publish": {
        "publishDate": "2019-10-25T12:25:47.938Z",
    }
}
]

const myParseDate = date_string => {
let [y,M,d,h,m,s] = date_string.split(/[- :T]/);
return new Date(y,parseInt(M)-1,d,h,parseInt(m),s.replace('Z',''));
}

arr.sort(function(a, b){    
if (a.publish  && b.publish)
    return myParseDate(b.publish.publishDate) - myParseDate(a.publish.publishDate)
else if (a.hasOwnProperty("publish"))
    return -1;
else if (b.hasOwnProperty("publish"))
    return 1;
else
    return 0;
} );

console.log(arr);

更新:

如果要升序,则只需更改aand的位置b

arr.sort(function(a, b){
    if (a.publish  && b.publish)
        return myParseDate(a.publish.publishDate) - myParseDate(b.publish.publishDate)
    else if (a.hasOwnProperty("publish"))
        return -1;
    else if (b.hasOwnProperty("publish"))
        return 1;
    else
        return 0;
} );

一个例子:

const arr = [
    {
        "id": "brexit-delay",
        "title": "Brexit Delay",
        "publish": {
            "publishDate": "2019-8-30T12:25:47.938Z",
        }
    },
    {
        "id": "brexit-delay",
        "title": "Example 3"
    },
    {
        "id": "brexit-delay",
        "title": "Example 2",
        "publish": {
            "publishDate": "2019-6-30T12:25:47.938Z",
        }
    },
    {
        "id": "brexit-delay",
        "title": "Example 5"
    },
    {
        "id": "brexit-delay",
        "title": "Example 5",
        "publish": {
            "publishDate": "2019-10-25T12:25:47.938Z",
        }
    }
]

const myParseDate = date_string => {
    let [y,M,d,h,m,s] = date_string.split(/[- :T]/);
    return  new Date(y,parseInt(M)-1,d,h,parseInt(m),s.replace('Z',''));
}

arr.sort(function(a, b){
    if (a.publish  && b.publish)
        return myParseDate(a.publish.publishDate) - myParseDate(b.publish.publishDate)
    else if (a.hasOwnProperty("publish"))
        return -1;
    else if (b.hasOwnProperty("publish"))
        return 1;
    else
        return 0;
} );

console.log(arr);


推荐阅读