And if I failed to mention it...
Calling the native sort() and passing in the sort ordering function is all going to one heluva lot faster than mucking with unshift and shift and all the rest.
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.