View Full Version : Sorting an array
snowtown
04-29-2003, 05:41 PM
Hi!
I'm having trouble sorting an array (using the sort() function) looking like this:
alert( ["foo 8","foo 10","foo 7","a1b2"].sort() );
The above results in "foo 10" being at index 1 instead of index 3.
Any help would be appreciated.
Thanks!
tamienne
04-29-2003, 05:53 PM
Actually it is sorting it correctly. 1 comes before 7 & 8. sort() does character sorts. You probably need to either add a 0 in front of 7 & 8 or maybe a space might work.
snowtown
04-29-2003, 06:00 PM
Ok, thanks, as I suspected then. Does anybody know a solution in sorting this kind of array "correctly"?
:)
liorean
04-29-2003, 06:10 PM
Sort is lexographical and for strings will compare ASCII values. That means 'B' sorts before 'a' and '10' sorts before '7' if you use it au naturel. However, as <http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/array.html#1196882> shows, the sort function takes a comparison function as it's argument, and you can design the comparison function to do what you want. The comparison function should take two arguments (let's call then a and b when we speak of them), and return <0 if a sorts lower than b, 0 if they sort equal, or >0 if a sorts higher than b.
snowtown
04-29-2003, 06:51 PM
Tackar! ;)
cheesebagpipe
04-29-2003, 07:03 PM
Not sure how durable this is:
<script type="text/javascript" language="javascript">
var sort_func = function(a,b) {
return Number(a.match(/\-?\d+/)) > Number(b.match(/-?\d+/)) ? 1 : -1;
}
alert( ["foo 8","foo 84","foo 0","foo -76","foo 4","foo 4","foo 10","foo -7","a1b2"].sort(sort_func));
</script>
Tails
04-29-2003, 07:45 PM
Nice, but is there a way to kill an entry in an array? I want to be able to have a array like:
X=Array("A","B","C")
and somehow, remove B, making A the 0th entry and C the 1st. I've looked at some methods such as split and splice and both did nothing. What do those do? And how to I remove array entries?
beetle
04-29-2003, 08:36 PM
Although the below code works, it's not super compatible, because Array.splice() requires IE 5.5+ and Array.push() requires IE6. Of course, those methods can be prototyped too, for the browsers that don't natively support them.Array.prototype.remove = function( value )
{
var i = this.indexOf( value );
var temp = this.splice( i, this.length ).slice( 1 );
for ( i = 0; i < temp.length; i++ )
{
this.push( temp[i] );
}
return this.length;
}
Array.prototype.indexOf = function( value )
{
var i = this.length;
while ( i-- > 0 )
{
if ( this[i] == value ) return i;
}
return -1;
}
var arr = ['a','b','c','d','e'];
alert( arr );
arr.remove( 'b' );
alert( arr );
liorean
04-29-2003, 09:45 PM
Beetle:
Huh? I have used Array.push in iew5.5 and it worked all fine. Array.splice on the other hand doesn't exist in iew5.5 if I remember correct.
Why don't you use Array.concat instead of that loop? It's bound to be faster.
I see you've adopted my use of while(i-->0) loops...
Tails:
Array.split doesn't exist. (Did you mean String.split, perhaps?)
Array.slice(iStart, iEnd) returns the array of elements from iStart to iEnd of an array. If you leave iEnd out, it will give all elements to the end of the array.
Array.splice(iStart, iNumber, eElement...) replaces iNumber number of elements beginning from iStart with the elements specified. If no replacement elements are specified, it will just cut out the specified elements. If iNumber is zero, it will add elements before iStart. See <http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/array.html#1193766>
Also, remember that these methods aren't mutating the object - they just return a new one. This means that you'll have to do an assignment to X if you want to replace it.
Tails
04-30-2003, 09:31 PM
Well I'm screwed. I have IE 5.0 and I can't update it for 3 reasons:
No internet connection available
Microsoft never distributed any version of IE beyond 4.0 as a redlist download (on a cd for any system)
My cd-drive is illiterate...It can't read Cds anymore :(
beetle
04-30-2003, 09:43 PM
Sorry, maybe I had the splice/push backwards there.
I was gonna use Array.concat() -- but I wanted my method to be a mutation, and concat wouldn't allow me to do that.
The while loop thing - ya - handy :D
Tails -- never fear, accomplishing this w/o a mutation is easy, it just requires that you use an assignment statement -- as liorean pointed out.Array.prototype.remove = function( value )
{
var i = this.length;
var temp = [];
while ( i-- > 0 )
{
if ( this[i] == value ) continue;
temp[temp.length] = this[i];
}
return temp.reverse();
}
var arr = ['a','b','c','d','e'];
alert( arr );
arr = arr.remove( 'b' );
alert( arr );
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.