【问题标题】:Sort list of items by specific status按特定状态对项目列表进行排序
【发布时间】:2022-01-26 00:02:35
【问题描述】:

我想按状态对任务列表进行排序,例如,状态为“活动”的任务应按照已完成的任务进行分组,然后其他状态的任务应按添加顺序显示:

它应该显示如下内容:

 const taskToShows = [
    {task: "task7", status: "active"},
    {task: "task6", status: "active"},
    {task: "task5", status: "active"},
    {task: "task8", status: "completed"},
    {task: "task4", status: "completed"},
    {task: "task1", status: "pending"},
    {task: "task2", status: "other status"},
    {task: "task3", status: "progress"},
   ]

我的方法是使用 sort() 我可以先排序,但随后它排序了所有任务,只需要顶部的活动和已完成的任务以及用户添加的顺序中的所有其他任务

if(this.status === 'active' || this.status === 'completed'){  
      this.tasksToShow = this.tasksList.sort((a, b) => (a.status.toLowerCase() > 
      b.status.toLowerCase()) ? 1 : (a.status.toLowerCase()  === b.status.toLowerCase() ) 
      ? ((a.status > b.status) ? 0 : -1) : -1 )
     } 

【问题讨论】:

    标签: javascript sorting


    【解决方案1】:

    试试这个-

     const taskToShows = [
        {task: "task7", status: "active"},
        {task: "task6", status: "active"},
        {task: "task5", status: "active"},
        {task: "task8", status: "completed"},
        {task: "task4", status: "completed"},
        {task: "task1", status: "pending"},
        {task: "task2", status: "other status"},
        {task: "task3", status: "progress"},
    ];
    
    
    
    taskToShows.sort((a, b) => {
      const arr = ['active', 'completed'];
      return arr.includes(a.status) || arr.includes(b.status) ? 1 : -1;
    });
    
    console.log(taskToShows);
    .as-console-wrapper{min-height: 100%!important; top: 0}

    【讨论】:

      【解决方案2】:
      • 使用Array#reduce,迭代数组,同时更新Map,其初始值为三个空列表:activecompletedother。在每次迭代中,从当前状态中获取对应的列表,other 作为 fallback,并将当前元素推送给它。
      • 使用Map#values,获取三个列表
      • 使用Array#flat,返回一个统一列表,其中首先显示active,然后是completed,然后是other

      const taskToShows = [ {task: "task2", status: "other status"}, {task: "task7", status: "active"}, {task: "task5", status: "active"}, {task: "task8", status: "completed"}, {task: "task1", status: "pending"}, {task: "task3", status: "progress"}, {task: "task4", status: "completed"}, {task: "task6", status: "active"} ];
      
      const res = [...
        taskToShows.reduce((lists, e) => {
          const list = lists.get(e.status) ?? lists.get('other');
          list.push(e);
          return lists;
        }, new Map([ ['active', []], ['completed', []], ['other', []] ]))
        .values()
      ].flat();
      
      console.log(res);

      【讨论】:

        【解决方案3】:

        当然你可以使用 sort():

        const taskToShows = [{task: "task7", status: "completed"},{task: "task6", status: "active"},{task: "task5", status: "active"},{task: "task8", status: "completed"},{task: "task4", status: "completed"},{task: "task1", status: "pending"},{task: "task2", status: "other status"},{task: "task3", status: "progress"},{task: "task33", status: "active"},{task: "task77", status: "completed"}];
        
        const statusOrder = {active: 0, completed: 1};
        
        const result = taskToShows.sort(
          ({status: s1}, {status: s2}) => (statusOrder[s1] ?? Infinity) - (statusOrder[s2] ?? Infinity)
        );
        
        console.log(taskToShows);
        .as-console-wrapper{min-height: 100%!important; top: 0}

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-10-29
          • 2017-04-08
          • 2022-01-18
          • 2021-05-22
          • 1970-01-01
          • 1970-01-01
          • 2013-01-09
          • 2011-06-17
          相关资源
          最近更新 更多