Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading