Originally Posted by 123jo
Thanks for your answers! But, @Old Pedant, I don't really understand what does:
function sortOnNumeric( a, b )
return Number( b.replace(/\D/g,"") ) - Number( a.replace(/\D/g,"") );
will replace all *NON DIGITS* (\D means non-digit, the /g means "all") with a blank string. So if b was "Z7X3JJ" all except the 7 and the 3 would be replace and the results would be "73". Do that to both of the arguments, subtract one from the other, and you have the classic way of sorting numbers. [The calls to Number( )
really aren't needed, since the subtraction will force conversion of "73" et al. to number 73 et al. anyway.]
And I don't want to use .sort...(the execution speed isn't an issue)
WHY? "Don't want to..." isn't a reason.
Basically, what I'm looking to do is to see if the string I'm about to add has an integer bigger than the first one in the result array. If it does, put it at the beginning of the array; if not, push it at the end. I can't think of any other way to sort an array in descending order without using .sort
Pardon me, but that's NONSENSE. That will *NOT* sort the array!
Take this example:
Add the number 7, 23, 19 to the array using your methodology:
-- Start with the array empty: [ ]
Add 7: Nothing in the array, so 7 is the first element.
-- The array is now .
Add 23: 23 is bigger than the first element (7), so it goes to front.
-- Array is now [23,7].
Add 19: 19 is *NOT* bigger than the first element (23),
so ACCORDING TO YOUR RULES push it to the end.
-- The array is now [23, 7, 19].
YOU ALREADY HAVE THE WRONG ANSWER!