数组列表按布尔值排序,然后按日期 JavaScript / TypeScript

Posted

技术标签:

【中文标题】数组列表按布尔值排序,然后按日期 JavaScript / TypeScript【英文标题】:Array List Sort by Boolean then by Date JavaScript / TypeScript 【发布时间】:2021-09-30 21:36:47 【问题描述】:

我正在动态地将新项目添加到列表中并在之后对其进行排序 先根据状态,再根据日期。工作代码

每当我向列表中添加新项目时,我希望列表显示具有最新日期的错误状态项目,然后按日期降序显示真实值项目

added item ="Status":false,"Date":"2021-07-23T07:43:01.377Z","Title":"test6"

无序列表:-

var itemlist = [
"Status":true,"Date":"2021-07-23T07:28:23.841Z","Title":"test5",
"Status":false,"Date":"2021-07-23T07:03:12.736Z","Title":"test4",
"Status":false,"Date":"2021-07-23T07:02:01.901Z","Title":"test3",
"Status":true,"Date":"2021-07-23T06:46:34.614Z","Title":"test2",
"Status":false,"Date":"2021-07-22T14:33:41.351Z","Title":"test1",
"Status":true,"Date":"2021-07-16T06:28:41.568Z","Title":"Test0"];

预期结果:-

var itemlist = [
    "Status":false,"Date":"2021-07-23T07:43:01.377Z","Title":"test6",
    "Status":false,"Date":"2021-07-23T07:03:12.736Z","Title":"test4",
    "Status":false,"Date":"2021-07-23T07:02:01.901Z","Title":"test3",
    "Status":false,"Date":"2021-07-22T14:33:41.351Z","Title":"test1",
    "Status":true,"Date":"2021-07-23T07:28:23.841Z","Title":"test5",
    "Status":true,"Date":"2021-07-23T06:46:34.614Z","Title":"test2",
    "Status":true,"Date":"2021-07-16T06:28:41.568Z","Title":"Test0"];

得到的结果:-

var itemlist = [
    "Status":false,"Date":"2021-07-23T07:03:12.736Z","Title":"test4",
    "Status":false,"Date":"2021-07-23T07:02:01.901Z","Title":"test3",
    "Status":false,"Date":"2021-07-22T14:33:41.351Z","Title":"test1",
    "Status":false,"Date":"2021-07-23T07:43:01.377Z","Title":"test6",
    "Status":true,"Date":"2021-07-23T07:28:23.841Z","Title":"test5",
    "Status":true,"Date":"2021-07-23T06:46:34.614Z","Title":"test2",
    "Status":true,"Date":"2021-07-16T06:28:41.568Z","Title":"Test0"];

使用方法:-

var res=itemlist.sort(
   (a, b) => (a.Status === b.Status ) ? 
      0 : (a.Status ? 1 : -1 || +new Date(b.Date)- +new Date(a.Date)));

【问题讨论】:

【参考方案1】:

你可以排序

Status 增量值递增,并且 Date 作为字符串降序。

var itemlist = [ Status: true, Date: "2021-07-23T07:28:23.841Z", Title: "test5" ,  Status: false, Date: "2021-07-23T07:03:12.736Z", Title: "test4" ,  Status: false, Date: "2021-07-23T07:02:01.901Z", Title: "test3" ,  Status: true, Date: "2021-07-23T06:46:34.614Z", Title: "test2" ,  Status: false, Date: "2021-07-22T14:33:41.351Z", Title: "test1" ,  Status: true, Date: "2021-07-16T06:28:41.568Z", Title: "Test0" ,  Status: false, Date: "2021-07-23T07:43:01.377Z", Title: "test6" ];

itemlist.sort((a, b) => a.Status - b.Status || b.Date.localeCompare(a.Date));

console.log(itemlist);
.as-console-wrapper  max-height: 100% !important; top: 0; 

【讨论】:

感谢您的快速回复。工作正常谢谢【参考方案2】:

您可以在sort 回调中使用逻辑或运算符。

var itemlist = [
"Status":true,"Date":"2021-07-23T07:28:23.841Z","Title":"test5",
"Status":false,"Date":"2021-07-23T07:03:12.736Z","Title":"test4",
"Status":false,"Date":"2021-07-23T07:02:01.901Z","Title":"test3",
"Status":true,"Date":"2021-07-23T06:46:34.614Z","Title":"test2",
"Status":false,"Date":"2021-07-22T14:33:41.351Z","Title":"test1",
"Status":true,"Date":"2021-07-16T06:28:41.568Z","Title":"Test0"];
itemlist.sort((a,b) => a.Status - b.Status || new Date(b.Date) - new Date(a.Date))
console.log(itemlist);

【讨论】:

【参考方案3】:

首先,Unordered list:-Expected Result:- 在示例数据中的元素数量不同。当您寻求帮助时,请尝试正确获取示例数据。

解决方案:

var compareFn = (a, b) => 
    if (a.Status !== b.Status) 
        return a.Status ? 1 : -1;
     else 
        return new Date(b.Date) < new Date(a.Date) ? -1 : 1;
    


var res=itemlist.sort(compareFn);

说明:

如果.Status 不匹配,我们首先希望元素为false,否则如果.Status 匹配,我们希望比较日期。

【讨论】:

以上是关于数组列表按布尔值排序,然后按日期 JavaScript / TypeScript的主要内容,如果未能解决你的问题,请参考以下文章

Ruby 按布尔值和数字排序

Javascript - 如何按 3 种不同的属性类型对对象数组进行排序? (字符串、整数、布尔值)

Javascript - 按日期然后按时间对对象数组进行排序

Firebase 根据日期按点排序

javascript 使用日期值按单键对对象数组进行排序

MySQL 按日期排序和布尔优先级:数字到日期的怪异