Created
May 27, 2022 09:14
-
-
Save chestozo/f76de0b2ef967f7e5232916b2fe3464f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const added = [ 5, 6, 4 ] | |
const ar = [ | |
{ id: 6, name: 'zzz' }, | |
{ id: 1, name: 'aaa' }, | |
{ id: 2, name: 'ccc' }, | |
{ id: 5, name: 'yyy' }, | |
{ id: 3, name: 'bbb' }, | |
{ id: 4, name: 'xxx' }, | |
] | |
const sorter = (a,b) => { | |
const i1 = added.indexOf(a.id) | |
const i2 = added.indexOf(b.id) | |
if (i1 > -1 && i2 > -1) { | |
return i2 - i1 | |
} | |
if (i1 > -1) { | |
return -1 | |
} | |
if (i2 > -1) { | |
return 1 | |
} | |
return a.name.localeCompare(b.name) | |
} | |
console.log(ar.sort(sorter)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sample output
