按日期排序待办事项列表,然后按状态排序

t0ybt7op  于 2021-09-23  发布在  Java
关注(0)|答案(5)|浏览(332)

我有一张待办事项清单,我需要按清单排序 dueDate 和按状态(未完成=>complete)。未完成的任务应在截止日期前按asc顺序排列。然后,完成的任务应该在未完成的任务之后,并且它们也应该按asc顺序排列 dueDate .

unsorted array 
[{"task":"Dishes", "isComplete":false, "dueDate":"2021-01-01T15:45:00.000Z"},{"task":"feed baby", "isComplete":true, "dueDate":"2021-01-02T15:45:00.000Z"},{"task":"Laundry", "isComplete":true, "dueDate":"2021-01-04T15:45:00.000Z"},{"task":"Homework", "isComplete":false, "dueDate":"2021-01-03T15:45:00.000Z"}]

sorted array
    [
    {
    "task":"Dishes",
    "isComplete":false,
    "dueDate":"2021-01-01T15:45:00.000Z"
    },
    {
    "task":"Homework",
    "isComplete":false,
    "dueDate":"2021-01-03T15:45:00.000Z"
    }
    {
    "task":"feed baby",
    "isComplete":true,
    "dueDate":"2021-01-02T15:45:00.000Z"
    },
    {
    "task":"Laundry",
    "isComplete":true,
    "dueDate":"2021-01-04T15:45:00.000Z"
    },
    ]

以下是我使用2个排序函数的方法,首先按日期,然后按状态,但我觉得可以进一步减少:

const sortByDate = (a,b) => {
    if (new Date(a.dueDate) < new Date(b.dueDate)){
        return -1;
    }
    if (new Date(a.dueDate) > new Date(b.dueDate)) {
        return 1;
    }
    return 0;
}
const sortByStatus = (a,b) => {
    if (a.isComplete < b.isComplete){
        return -1;
    }
    if (a.isComplete > b.isComplete) {
        return 1;
    }
    return 0;
}
e5nqia27

e5nqia271#

试试这个:

var array = [{"task":"Dishes", "isComplete":false, "dueDate":"2021-01-01T15:45:00.000Z"},{"task":"feed baby", "isComplete":true, "dueDate":"2021-01-02T15:45:00.000Z"},{"task":"Laundry", "isComplete":true, "dueDate":"2021-01-04T15:45:00.000Z"},{"task":"Homework", "isComplete":false, "dueDate":"2021-01-03T15:45:00.000Z"}]

var newarray = array.sort((a, b) => {
  const date = new Date(a.dueDate) - new Date(b.dueDate);
  const isComplete = a.isComplete - b.isComplete;

  return isComplete || date ;
})

console.log(newarray);
ecr0jaav

ecr0jaav2#

您可以使用减法的强制属性和逻辑or运算符。

const arr = [{"task":"Dishes", "isComplete":false, "dueDate":"2021-01-01T15:45:00.000Z"},{"task":"feed baby", "isComplete":true, "dueDate":"2021-01-02T15:45:00.000Z"},{"task":"Laundry", "isComplete":true, "dueDate":"2021-01-04T15:45:00.000Z"},{"task":"Homework", "isComplete":false, "dueDate":"2021-01-03T15:45:00.000Z"}];
arr.sort((a,b) => a.isComplete - b.isComplete || new Date(a.dueDate) - new Date(b.dueDate));
console.log(arr);
deyfvvtc

deyfvvtc3#

如果您要求使用组合排序功能。那么这个就可以了

const sortArray = (a,b) => {
    if (new Date(a.dueDate) < new Date(b.dueDate)){
        return -1;
    }
    else if (new Date(a.dueDate) > new Date(b.dueDate)) {
        return 1;
    }
    else {  // This else means, due date is same, and we need to consider the status in this case
        if (b.isComplete){
            return 1;
        }
        else if (a.isComplete) {
            return -1;
        }
        return 0; // This means status and due date are equal
    }
};

首先,这将按到期日排序。如果到期日相等,则将考虑此类要素的状态。

zujrkrfu

zujrkrfu4#

array.sort((a, b) => {
  return sortByStatus(a,b) || sortByDate(a, b);
});

更具描述性

array.sort((a, b) => {
  const byStatus = sortByStatus(a, b)
  // if status isn't same, compare by status
  if( byStatus !== 0 ) return byStatus;
  // else compare by date
  return sortByDate(a,b);
});
xtfmy6hx

xtfmy6hx5#

试试这个。

unsorted_arr =
[{"task":"Dishes", "isComplete":false, "dueDate":"2021-01-01T15:45:00.000Z"},{"task":"feed baby", "isComplete":true, "dueDate":"2021-01-02T15:45:00.000Z"},{"task":"Laundry", "isComplete":true, "dueDate":"2021-01-04T15:45:00.000Z"},{"task":"Homework", "isComplete":false, "dueDate":"2021-01-03T15:45:00.000Z"}];

unsorted_arr.sort(function (a, b) {
    if(a.isComplete == b.isComplete)
        return (a.dueDate < b.dueDate) ? -1 : (a.dueDate > b.dueDate) ? 1 : 0;
    else
        return (a.isComplete < b.isComplete) ? -1 : 1;
});
console.log(unsorted_arr);

相关问题