View Full Version : Array sortOn problems with numbers
Spongebob
August 9th, 2002, 05:39 PM
I've customized this sortOn example from Flash Help:
var recArray = new Array();
recArray.push( { nprod: "Prod4", vprod:15} );
recArray.push( { nprod: "Prod1", vprod:250} );
recArray.push( { nprod: "Prod5", vprod:36} );
recArray.push( { nprod: "Prod2", vprod:100} );
recArray.push( { nprod: "Prod3", vprod:5} );
recArray.push( { nprod: "Prod4", vprod:8} );
and then sorted the Array on vprod (numeric value):
recArray.sortOn("vprod");
recArray.reverse();
for(var i=0; i<recArray.length;i++){
trace(recArray[i].vprod);
}
(I don't know why this last for sentence cannot be seen correctly in Preview Mode but it's not an error, it's working fine...)
And the output is:
8
5
36
250
15
100
!!!!!!! ?????? Shouldn't it be ordered in descendent order ?? That's what I'm trying to do without any result.
I tried to load the values (for each object) as an integer this way:
recArray.push( { nprod: "Prod4", vprod:parseInt(15)} );
... but it doesn't work either...
Could you give me a little help ?
Thanks in advance,
Spongebob
Iammontoya
August 11th, 2002, 12:10 PM
Hello, friend.
Here's your answer... you dont want to use sort on in this case, for sortOn is looking for a fieldname.. as in
team=[name: "bobby", name: "susy"]..
instead, you want to use array.sort, which is a bit of a tricky beast. For adequate results, you need to build a little function that will tell array.sort how you want your items sorted.
function(AtoZ){
return a -b;
}
now, you can do... array.sort(AtoZ), and that will give you the numerical values in the order you want (ascending)... if you want descending...
return b - a
There you go! Good luck!
Spongebob
August 12th, 2002, 09:42 AM
Hi friend,
Thanks for answering :)
I think I don't understand how to do it right yet...
I want my Objects Array sorted on vprod.
recArray.push( { nprod: "Prod4", vprod:15} );
recArray.push( { nprod: "Prod1", vprod:250} );
recArray.push( { nprod: "Prod5", vprod:36} );
You're telling me to create a function so as to tell array.sort how I want my elements sorted.
Suppose I want vprods in descending order:
I should define:
function(ZtoA){ // Shouldn't this function have a name ???
return b - a;
}
How do I "tell" the sort method to use this function ?
You told me: array.sort(ZtoA) but I don't understand... how does it know that it should sort on vprod instead of nprod ?
This is not very clear to me... :( Could you give me an example ?
Thanks in advance,
Spongebob.
pom
August 12th, 2002, 10:14 AM
Glups! I've tried the example from MM, and ever theirs doesn't work well!! About Inigo's technique, you can use pretty much the same example they give in the Help. But that thing with sortOn is really strange.
pom :asian:
pom
August 12th, 2002, 10:20 AM
Mmm... macromedia's example seems to work now... I don't get it:(
pom :asian:
Spongebob
August 12th, 2002, 10:23 AM
I found this at:
http://www.actionscript-toolbox.com/arrayobject2.php
function sortByNumber(a, b) {
return (a > b);
}
ages = [13, 52, 33, 2, 25, 14, 3, 77, 8];
ages.sort(sortByNumber);
trace(ages);
output --> 2,3,8,13,14,25,33,52,77
Works fine but I don't know how to apply it to my Object's array
which has two properties: nprod and vprod. I want to sort on numerical value vprod.
recArray.push( { nprod: "Prod4", vprod:15 } );
recArray.push( { nprod: "Prod5", vprod:200} );
Any idea ?
Thanks,
Spongebob
Spongebob
August 12th, 2002, 10:36 AM
Found it !!! =)
Take a look at:
http://www.actionscript-toolbox.com/arrayobject3.php
Section: "Sorting an array of objects"
Hope it's useful for you all,
Thanks,
Spongebob
Iammontoya
August 12th, 2002, 11:04 AM
I think I understood the code until I went to that site...
AtoZ and ZtoA are arbitrary names. I just made the names up to help me remember what I was trying to do.
The basic issue with function is that you will tell it in the function where the item should go by assigning values.
if we're comparing a vs b or item1 vs item2
-1 (first item goes before 2nd item)
1 (first item goes after 2nd item)
0 (no change, or items are equal)
that way
function lowertohigher (a,b){
if(a<b){
return -1;
} else if(a>b){
return 1;
} else {
return 0;
}
}
in your case, instead a, b should correspond to the array name you want to use, with it's field name.
Make sense?
pom
August 12th, 2002, 11:17 AM
Very interesting, but it doesn't explain why the sortOn method doesn't work :*(
pom :asian:
Spongebob
August 12th, 2002, 11:24 AM
Yes, it makes sense :)
Thanks Inigo !
I'm wondering why the normal MM sortOn method doesn't work for numbers too :-\ ... Think it should be fixed! :evil:
Maybe for next version :)
Spongebob
Iammontoya
August 12th, 2002, 10:56 PM
sortOn did work, if you take a closer look. Problem is.. it converts your values to a string. so if you sort
14, 20, 100, 2000, 4
it will give you
100
14
20
20000
4
because 1's go before 2's and so on and so forth..
pom
August 13th, 2002, 01:15 AM
Well observed, Inigo! Actually, I asked that question to my Chinese master, and he told me the same thing. So to make things straight, you'd have to enter the number as strings of the same length in the first place:
vprod="004";
vprod="075";
etc.
pom :asian:
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.