# Array sortOn problems with numbers

**URL:** <https://forum.kirupa.com/t/array-sorton-problems-with-numbers/4455>\
**Category:** flash\
**Created:** [August 9, 2002, 9:39pm UTC](https://forum.kirupa.com/t/array-sorton-problems-with-numbers/4455 "2002-08-09T21:39:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spongebob](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@Spongebob](https://forum.kirupa.com/u/Spongebob)\
**Post date:** [August 9, 2002, 9:39pm UTC](https://forum.kirupa.com/t/array-sorton-problems-with-numbers/4455/1 "2002-08-09T21:39:41Z")

</div>

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

[COLOR=blue]recArray.sortOn(“vprod”);[/COLOR]  
[COLOR=orange]recArray.reverse();[/COLOR]

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

```
    [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,  
Spongebob

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 11, 2002, 4:10pm UTC](https://forum.kirupa.com/t/array-sorton-problems-with-numbers/4455/2 "2002-08-11T16:10:44Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 12, 2002, 1:42pm UTC](https://forum.kirupa.com/t/array-sorton-problems-with-numbers/4455/3 "2002-08-12T13:42:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 12, 2002, 2:14pm UTC](https://forum.kirupa.com/t/array-sorton-problems-with-numbers/4455/4 "2002-08-12T14:14:53Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 12, 2002, 2:20pm UTC](https://forum.kirupa.com/t/array-sorton-problems-with-numbers/4455/5 "2002-08-12T14:20:07Z")

</div>

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

pom :asian:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 12, 2002, 2:23pm UTC](https://forum.kirupa.com/t/array-sorton-problems-with-numbers/4455/6 "2002-08-12T14:23:06Z")

</div>

I found this at:  
[http://www.actionscript-toolbox.com/arrayobject2.php](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”, [COLOR=orangered]vprod:15[/COLOR] } );  
recArray.push( { nprod: “Prod5”, [COLOR=orangered]vprod:200[/COLOR]} );

Any idea ?  
Thanks,  
Spongebob

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 12, 2002, 2:36pm UTC](https://forum.kirupa.com/t/array-sorton-problems-with-numbers/4455/7 "2002-08-12T14:36:19Z")

</div>

Found it !!! =)

Take a look at:  
[http://www.actionscript-toolbox.com/arrayobject3.php](http://www.actionscript-toolbox.com/arrayobject3.php)

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

Hope it’s useful for you all,  
Thanks,  
Spongebob

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 12, 2002, 3:04pm UTC](https://forum.kirupa.com/t/array-sorton-problems-with-numbers/4455/8 "2002-08-12T15:04:19Z")

</div>

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

```auto

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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 12, 2002, 3:17pm UTC](https://forum.kirupa.com/t/array-sorton-problems-with-numbers/4455/9 "2002-08-12T15:17:32Z")

</div>

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

pom :asian:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 12, 2002, 3:24pm UTC](https://forum.kirupa.com/t/array-sorton-problems-with-numbers/4455/10 "2002-08-12T15:24:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 13, 2002, 2:56am UTC](https://forum.kirupa.com/t/array-sorton-problems-with-numbers/4455/11 "2002-08-13T02:56:05Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 13, 2002, 5:15am UTC](https://forum.kirupa.com/t/array-sorton-problems-with-numbers/4455/12 "2002-08-13T05:15:16Z")

</div>

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:
