Skip to content

Instantly share code, notes, and snippets.

@chestozo
Created May 27, 2022 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chestozo/f76de0b2ef967f7e5232916b2fe3464f to your computer and use it in GitHub Desktop.
Save chestozo/f76de0b2ef967f7e5232916b2fe3464f to your computer and use it in GitHub Desktop.
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))
@chestozo
Copy link
Author

sample output
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment