简体   繁体   English

根据值对数组重新排序

[英]Reorder array based on values

I have an array with classes (the class is called Note ), and in those classes are basic values.我有一个包含类的数组(该类称为Note ),并且在这些类中是基本值。 Two important values are the Note.Pinned (boolean whether it's pinned or not) and Note.Modified (a timestamp when it was last edited).两个重要的值是Note.Pinned (布尔值,无论是否固定)和Note.Modified (上次编辑时的时间戳)。

I would like to sort this array of classes by the Modified timestamp, then reorder it that the Pinned notes are on top of this list.我想按Modified时间戳对这个类数组进行排序,然后重新排序, Pinned笔记位于此列表的顶部。

I already tried sorting the notes.我已经尝试整理笔记。

const SortedNotes = Notes.sort((a, b) => a.Modified > b.Modified);

Notes being the array with classes. Notes是带有类的数组。

But, then, is there a better way to rearrange this array?但是,有没有更好的方法来重新排列这个数组? Or is this the only method I can use?或者这是我可以使用的唯一方法?

SortedNotes.filter(n => n.Pinned).concat(SortedNotes.filter(n => !n.Pinned));

The above would work, and I know I can use Array.prototype.partition , so is there any other way of doing this?上面的方法可行,我知道我可以使用Array.prototype.partition ,那么还有其他方法可以做到这一点吗?

Thanks谢谢

You can just add the pinned sorting requirement to the sort:您可以将固定排序要求添加到排序中:

 const arr = [{ pinned: false, modified: 7 }, { pinned: false, modified: 6 }, { pinned: true, modified: 2 }, { pinned: false, modified: 4 }, { pinned: true, modified: 1 }, { pinned: true, modified: 8 }, { pinned: false, modified: 3 }, ] arr.sort((a, b) => { if (a.pinned != b.pinned) return a.pinned ? -1 : 1; return b.modified - a.modified; }) console.log(arr)

As long as the execution of the sort function returns the same result for the same two given parameters the sort will work fine.只要排序函数的执行对相同的两个给定参数返回相同的结果,排序就可以正常工作。 Knowing this, you can handle the two states where the pinned state is different and then sort by modified only if the pinned state is the same.知道了这一点,您可以处理固定状态不同的两种状态,然后仅当固定状态相同时才按修改进行排序。

Not the best solution perhaps, but this should help you in the right direction.也许不是最好的解决方案,但这应该可以帮助您朝着正确的方向前进。 This sorts the array so that notes are in decending date order, with the pinned ones at the top.这会对数组进行排序,以便笔记按日期降序排列,固定的位于顶部。

Unlike other (better) answers, this has a O(2n) sorting complexity, so bear that in mind.与其他(更好的)答案不同,这具有 O(2n) 排序复杂度,因此请记住这一点。

 const notes = [ { text: "foo", modified: 1583337610387, pinned: false }, { text: "bar", modified: 1583337610388, pinned: true }, { text: "baz", modified: 1583337610389, pinned: false }, { text: "qux", modified: 1583337610390, pinned: true } ]; const sortedNotes = [...notes]; sortedNotes.sort((a, b) => a.modified > b.modified ? -1 : 1); sortedNotes.sort((a, b) => (a.pinned === b.pinned) ? 0 : a.pinned ? -1 : 1); console.log(sortedNotes);

You can sort by multiple conditions:您可以按多个条件排序:

const SortedNotes = Notes.sort((a, b) => {
  if (a.Pinned == b.Pinned) {
    if (a.Modified > b.Modified) return -1;
    if (a.Modified < b.Modified) return 1;
  } else {
    return a.Pinned ? -1 : 1;
  }
});

The missing branch of a.Pinned == b.Pinned && a.Modified == b.Modified would return undefined , which in turn would be interpreted as equal. a.Pinned == b.Pinned && a.Modified == b.Modified的缺失分支将返回undefined ,这反过来将被解释为相等。

Btw, the array Notes itself would've been modified in place, so both SortedNotes and Notes would exhibit the same ordering;顺便说一句,数组Notes本身会被修改到位,因此SortedNotesNotes将显示相同的顺序; of course, assigning to a const variable can't hurt.当然,分配给const变量不会有什么坏处。

Shorter, you can try短一点,你可以试试

const SortedNotes = Notes.sort((a, b) => {
    if (a.Pinned > b.Pinned)
       return 1;
    if (a.Pinned == b.Pinned) 
       return a.Modified > b.Modified;
    return -1;
})

I edited @Ja͢ck's answer to be a little more compact.我编辑了@Ja͢ck 的答案,使其更加紧凑。

const SortedNotes = Notes.sort((a, b) => {
    if (a.Pinned == b.Pinned) return a.Modified > b.Modified ? -1 : 1;
    else return a.Pinned ? -1 : 1;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM