View Single Post
12-28-2012, 07:57 PM   PM User | #10
Old Pedant
Supreme Master coder!

Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Quote:
 Originally Posted by 123jo Thanks for your answers! But, @Old Pedant, I don't really understand what does: Code: ```function sortOnNumeric( a, b ) { return Number( b.replace(/\D/g,"") ) - Number( a.replace(/\D/g,"") ); }```
Okay...the .replace 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.]
Quote:
 And I don't want to use .sort...(the execution speed isn't an issue)
WHY? "Don't want to..." isn't a reason.

Quote:
 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:
Code:
```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 [7].
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!```
__________________
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.