If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Need help to sort an array of strings and numbers

Hi, I need to sort an array of strings like ["DOWNc18","OUTc11","INc21","UPc0","SIDEc33"] in descending order of the numbers of each strings.
At each step of my function, I add a string similar to those above in my array but it has to be in descending order. I do not want to use a sort function when all the elements of the array have been added. I want to sort one string at a time once it's added.

Considering I have the integer of the element I'm about to add, but not those already added, here's a little part of what I did:

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

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.]

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:

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.

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

Indeed, the integer is checked to see if it is bigger than the "first one" IN WHAT REMAINS OF THE ORIGINAL ARRAY (not the entire array, which is where 123jo really messed up). And, if it is, then that integer is put at the beginning OR THE REST of the original array. In other words, I fixed his broken algorithm by looking at the original array element by element instead of as a whole.

Last edited by Old Pedant; 12-29-2012 at 11:13 PM.

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.