Skip to content
Advertisement

Sort array into alternating smallest largest values?

Does anyone know how to sort an array into alternating smallest largest values?

I.E.

JavaScript

Should be :

JavaScript

EDIT:

Forgot to mention the arrays are associative, so:

JavaScript

Should become:

JavaScript

Advertisement

Answer

Sort your array then push elements from beginning and end of the array alternatively:

JavaScript

returns:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement