O
O
oskar barin2021-06-10 03:02:51
JavaScript
oskar barin, 2021-06-10 03:02:51

How to sort an array so that the elements are in the same order as in another array?

There is an array: You need to arrange its elements in the same order as in this array: And get the output:

["290", "999", "222", "333", "987", "309", "666"]


["999", "222", "666"]


["999", "222", "666", "290", "333", "987", "309"]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
asaiyo, 2021-06-10
@madrogalo

let allElems = ["290", "999", "222", "333", "987", "309", "666"];
let pattern = ["999", "222", "666"];
let goodElems = []; 
let badElems = [];

for (let elem of allElems){
  pattern.includes(elem) ? goodElems.push(elem) : badElems.push(elem);
}

let filteredElems = goodElems.concat(badElems);
console.log(filteredElems);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question