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:
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.
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,"") );
}
And I don't want to use .sort...(the execution speed isn't an issue)
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
If you want to sort without using sort() then you will need to implement your own sorting method.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
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.
Ehhh...it can be done simpler than that. Plus the following code handles them mixed text and numbers as in the original request. (Not that doing so would be hard with jmrker's code, of course.)
Code:
<script type="text/javascript">
function sortByNumberArray( )
{
this.tab = [ ];
this.getVal = function( txt )
{
return Number( txt.replace(/\D/g,"") );
}
this.addAndSort = function( addWhat )
{
var addVal = this.getVal( addWhat );
var newtab = [];
for ( var t = 0; t < this.tab.length; ++t )
{
if ( addWhat != null && addVal > this.getVal( this.tab[t] ) )
{
newtab.push( addWhat );
addWhat = null;
}
newtab.push( this.tab[t] );
}
if ( addWhat != null ) { newtab.push( addWhat ); }
this.tab = newtab;
return this.tab;
}
}
// and all the rest of this is to demonstrate use of the above
var sorter1 = new sortByNumberArray( );
var sorter2 = new sortByNumberArray( );
var toAdd1 = [ "DOWNc18","OUTc11","INc21","UPc0" ];
var toAdd2 = [ "SIDEc33", "F1GG4", "2V7", "XXX1Z9" ];
for ( var t = 0; t < toAdd1.length; ++t )
{
document.write( "sorter1: " + sorter1.addAndSort( toAdd1[t] ) + "<hr>" );
document.write( "sorter2: " + sorter2.addAndSort( toAdd2[t] ) + "<hr>" );
}
</script>
This is close to the spirit of what 123jo wrote when he said
Quote:
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.
__________________
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.
Last edited by Old Pedant; 12-29-2012 at 10:13 PM..