Move array's one element to the end position and another one to the front position, and the others should be in the remaining position

I have Array: [B, D, I, M, Other, T, U].

I want to sort it in order: [T, B, D, I, M, U, Other].

How can I implement it with JavaScript?

>Solution :

You could take an object with the order and sort the array.

const
    array = ['B', 'D', 'I', 'M', 'Other', 'T', 'U'],
    order = { T: -1, Other: Number.MAX_VALUE };

array.sort((a, b) => (order[a] || 0) - (order[b] || 0));

console.log(...array);

Leave a Reply