Array sortOn problems with numbers

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):


for(var i=0; i<recArray.length;i++){
(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:

!!! ??? 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:

    [COLOR=red]recArray.push( { nprod: "Prod4", vprod:parseInt(15)} );[/COLOR] 

… but it doesn’t work either…

Could you give me a little help ?

Thanks in advance,

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.

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!

Hi friend,
Thanks for answering :slight_smile:
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… :frowning: Could you give me an example ?

Thanks in advance,

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:

Mmm… macromedia’s example seems to work now… I don’t get it:(

pom :asian:

I found this at:

function sortByNumber(a, b) {
return (a > b);
ages = [13, 52, 33, 2, 25, 14, 3, 77, 8];

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”, [COLOR=orangered]vprod:15[/COLOR] } );
recArray.push( { nprod: “Prod5”, [COLOR=orangered]vprod:200[/COLOR]} );

Any idea ?

Found it !!! =)

Take a look at:

Section: [COLOR=red]“Sorting an array of objects”[/COLOR]

Hope it’s useful for you all,

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){

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?

Very interesting, but it doesn’t explain why the sortOn method doesn’t work :*(

pom :asian:

Yes, it makes sense :slight_smile:
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 :slight_smile:


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


because 1’s go before 2’s and so on and so forth…

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:

pom :asian: