Question: I have an array with classes (the class is called `Note`), and in those classes are basic values. Two important values are the `Note.Pinned` (boolean whether it's pinned or not) and `Note.Modified` (a timestamp when it was last edited).

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.

I already tried sorting the notes.

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

```

`Notes` being the array with classes.

But, then, is there a better way to rearrange this array? Or is this the only method I can use?

```js
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?

Thanks

Answer: 
You can just add the pinned sorting requirement to the sort:

```js
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.
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;
})

```