1

I need to sort my list using the selected array.

const list = [
    { name: 'BMW' },
    { name: 'AUDI' },
    { name: 'MINI' },
    { name: 'FIAT' },
    { name: 'KIA' },
]

const selected = [ 'MINI', 'KIA' ]

What is the best way to do this? I have found some examples but not too sure how to adapt this to work the way I want it to!

list.sort((a,b) => (a.last_nom > b.last_nom) ? 1 : ((b.last_nom > a.last_nom) ? -1 : 0))

Expected result:

const list = [
    { name: 'MINI' },
    { name: 'KIA' },
    { name: 'BMW' },
    { name: 'AUDI' },
    { name: 'FIAT' },
]
7
  • Shouldn't last_nom be name?
    – Barmar
    Commented Aug 23, 2021 at 19:54
  • 1
    Use selected.indexOf(a.name) and selected.indexOf(b.name), and compare those before comparing the values of a.name and b.name
    – Barmar
    Commented Aug 23, 2021 at 19:55
  • 1
    Does this answer your question? Sort an object array by custom order Commented Aug 23, 2021 at 19:56
  • @stevecomrie That's not quite the same, because the custom order there includes all the names. selected just has some ofthem.
    – Barmar
    Commented Aug 23, 2021 at 19:57
  • Does the order within the selected and non-selected groups matter?
    – Barmar
    Commented Aug 23, 2021 at 19:59

1 Answer 1

1

You could use Array.prototype.includes and take advantage of JavaScript's numbers coercion like this:

const list = [
  { name: "BMW" },
  { name: "AUDI" },
  { name: "MINI" },
  { name: "FIAT" },
  { name: "KIA" },
];

const selected = ["MINI", "KIA"];

const sorted = list.sort(
  (a, b) => selected.includes(b.name) - selected.includes(a.name)
);

console.log(sorted);

If the order of the elements has to remain the same and be consistent across browsers:

const list = [
  { name: "BMW" },
  { name: "AUDI" },
  { name: "MINI" },
  { name: "FIAT" },
  { name: "KIA" },
];

const selected = ["MINI", "KIA"];

const sorted = list.sort(
  (a, b) =>
    selected.includes(b.name) - selected.includes(a.name) ||
    list.indexOf(a) - list.indexOf(b)
);

console.log(sorted);

5
  • includes() just returns true or false, it doesn't really make sense to subtract them.
    – Barmar
    Commented Aug 23, 2021 at 19:56
  • Still it returns a number that represents the difference as if true were 1 and false were 0
    – Guerric P
    Commented Aug 23, 2021 at 19:57
  • I assumed that if both are in selected, they want them in the same order as in selected. This puts all the selected items first, non-selected items later, but doesn't specify the order within those groups.
    – Barmar
    Commented Aug 23, 2021 at 19:59
  • Indeed, chromium happens to preserve the order but it's implementation-dependent
    – Guerric P
    Commented Aug 23, 2021 at 20:01
  • Yeah as long as the selected are at the top that's all I need :D Commented Aug 23, 2021 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.