【问题标题】:How to sort array based on date in descending order if value exists in JS?如果JS中存在值,如何根据日期按降序对数组进行排序?
【发布时间】:2020-04-12 17:43:45
【问题描述】:

我正在尝试根据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)
    }
} );

【问题讨论】:

  • 您希望如何处理那些缺少publishDate 的对象?他们是一切还是
  • 另外,您正在检查'publish' in a,而原始对象中的键称为publishDate,所以我想它应该是'publishDate' in a
  • @SebastianKaczmarek publishDatepublish 的键,所以没关系。我假设如果publish 键存在,那么publish.publishDate 也会有一些日期值。
  • @SebastianKaczmarek 我想尽快处理没有publishDate 的对象
  • @hussain.codes 啊,对不起,你是对的。我没有看到publish 键;)

标签: javascript arrays node.js sorting


【解决方案1】:

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

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);

更新:

如果你想要升序,那么只需改变ab的位置:

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);

【讨论】:

  • 根据您的代码输出,Example 2 将排在第二位,但它应该排在第一位。
  • @Addy Example22019-6-30T12:25:47.938Z,但是 Brexit Delay2019-8-30T12:25:47.938Z 并且是降序的
  • 如果我想要asc order,我需要进行哪些更改
  • 您的代码在 js 执行中运行良好。但是我正在尝试在节点 js 中使用代码,但我遇到了没有日期的数组的问题。
  • @Addy 作为 stackoverflow 的一个好习惯,最好每个帖子有一个问题。你能创建一个有错误的新帖子吗?
【解决方案2】:

排序函数必须返回一个数字。您的排序比较函数中有一个带有和比较器的 if 语句,因此如果某个对象没有发布日期,则比较函数不会返回任何内容,您可以尝试放置更多 if

a.sort((a, b) => {
    if (a.publish && b.publish) {
        return //compare condition
    } else if (a.publish && !b.publish) {
        return //compare condition
    } else if (!a.publish && b.publish) {
        return //compare condition
    } else {
        return //compare condition
    }
});

【讨论】:

    【解决方案3】:

    您需要将代码更新为:

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

    new Date("2019-10-25T12:25:47.938Z") 只返回字符串“Invalid Date”,除非您替换“T”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-10
      • 2016-10-15
      • 2017-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      相关资源
      最近更新 更多