0

I have some array thats looks like this

let array = ['RANDOM', 'TEST', 'DSVEN','VEN','TEST','SVEN'];

What I need is to sort array based on his values, new array should look like this

let newArray = ['SVEN','DSVEN','VEN' **ETC.**];

it does not matter what order will be at the end, all I need is that array starts with those three elements. Keep in mind that initial array is just example it can me sorted different, but final result should be like newArray

I know I can pass function in sort, but I don't know what to check and how to sort, maybe i need some loop. I don't know the correct way, maybe sort can not even do this?

array.sort(comparefunction)

Thanks in advance

2
  • sounds like you need a 2-stage rocket. First do a regular sort, then add the fixed 3 elements to the start Commented Nov 25, 2022 at 13:00
  • const newArray = [...array]; for (const str of ['VEN', 'DSVEN', 'SVEN']) newArray.unshift(newArray.splice(newArray.indexOf(str), 1)[0]); Commented Nov 25, 2022 at 13:10

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.