Javascript 按调用方法返回的值对对象数组进行排序

我有一个包含值和方法的对象数组。其中一种方法返回我想要排序的数字。我还有一些布尔值,它们确定另一个排序顺序,无论返回的数字如何。


function reorder(){

    var pinnedTasks = []

    var regularTasks = []

    var finishedTasks = []


    for(var i of tasks){


        if(i.pinned){

            pinnedTasks.push(i)

        } else if(i.done){

            finishedTasks.push(i)

        } else{

            regularTasks.push(i)

        }


    }


}

这会将它们组织成类别,但不会重新排序。我想要一个包含pinnedTasks第一个、regularTasks下一个和最后一个的数组,所有这些都在自己的部分内finishedTasks重新排序。i.getDueDaysDiff()


具体来说,我希望它按正值递增(最小优先)的值排序i.getDueDaysDiff(),但负值递减(最大优先)排序。例如,[10, -5, -90, 2] 排序后应为 [2, 10, -5, -90]。


例如,如果我有


tasks = [

task: regular, getDueDaysDiff() = 1

task: regular, getDueDaysDiff() = -2

task: regular, getDueDaysDiff() = 12

task: regular, getDueDaysDiff() = -50

task: pinned, getDueDaysDiff() = -10

task: pinned, getDueDaysDiff() = 2

task: finished, getDueDaysDiff() = 5

]

重新排序应该是


tasks = [

task: pinned, getDueDaysDiff() = 2

task: pinned, getDueDaysDiff() = -10

task: regular, getDueDaysDiff() = 1

task: regular, getDueDaysDiff() = 12

task: regular, getDueDaysDiff() = -2

task: regular, getDueDaysDiff() = -50

task: finished, getDueDaysDiff() = 5

]

我应该如何处理这个问题?


qq_笑_17
浏览 95回答 1
1回答

紫衣仙女

我想我已经明白了:function reorder(){&nbsp; &nbsp; var pinnedTasksPos = []&nbsp; &nbsp; var pinnedTasksNeg = []&nbsp; &nbsp; var regularTasksPos = []&nbsp; &nbsp; var regularTasksNeg = []&nbsp; &nbsp; var finishedTasksPos = []&nbsp; &nbsp; var finishedTasksNeg = []&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for (e=0; e<tasks.length; e++){&nbsp; &nbsp; &nbsp; &nbsp; let i = tasks[e]&nbsp; &nbsp; &nbsp; &nbsp; console.log(i)&nbsp; &nbsp; &nbsp; &nbsp; if(i.pinned){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i.dueDaysDiff >= 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pinnedTasksPos.push(i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pinnedTasksNeg.push(i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else if(i.done){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i.dueDaysDiff >= 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finishedTasksPos.push(i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finishedTasksNeg.push(i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i.dueDaysDiff >= 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; regularTasksPos.push(i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; regularTasksNeg.push(i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; pinnedTasksPos.sort(compare)&nbsp; &nbsp; pinnedTasksNeg.sort(compare).reverse()&nbsp; &nbsp; regularTasksPos.sort(compare)&nbsp; &nbsp; regularTasksNeg.sort(compare).reverse()&nbsp; &nbsp; finishedTasksPos.sort(compare)&nbsp; &nbsp; finishedTasksNeg.sort(compare).reverse()&nbsp; &nbsp; var newarr = [...pinnedTasksPos, ...pinnedTasksNeg, ...regularTasksPos, ...regularTasksNeg, ...finishedTasksPos, ...finishedTasksNeg]&nbsp; &nbsp; return(newarr.reverse())}function compare(a, b){&nbsp; &nbsp; if(a.dueDaysDiff < b.dueDaysDiff){&nbsp; &nbsp; &nbsp; &nbsp; return -1&nbsp; &nbsp; }&nbsp; &nbsp; if(a.dueDaysDiff > b.dueDaysDiff){&nbsp; &nbsp; &nbsp; &nbsp; return 1&nbsp; &nbsp; }&nbsp; &nbsp; return 0}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript