# How can I sort an array with objects in?

**URL:** <https://forum.kirupa.com/t/how-can-i-sort-an-array-with-objects-in/64789>\
**Category:** flash\
**Created:** [September 20, 2004, 2:51am UTC](https://forum.kirupa.com/t/how-can-i-sort-an-array-with-objects-in/64789 "2004-09-20T02:51:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![OM2](https://avatars.discourse-cdn.com/v4/letter/o/3ab097/32.png) [@OM2](https://forum.kirupa.com/u/OM2)\
**Post date:** [September 20, 2004, 2:51am UTC](https://forum.kirupa.com/t/how-can-i-sort-an-array-with-objects-in/64789/1 "2004-09-20T02:51:23Z")

</div>

I want to sort an array that contains object references.  
I want to sort on one of the values held by the objects.

I have the following sample code, say:

```auto

   ActionScript:abc = new Array();
   
   abc[0] = new Object();
   abc[0].anotherObj = new Object();
   abc[0].anotherObj.myNumber = 21;
   
   abc[1] = new Object();
   abc[1].anotherObj = new Object();
   abc[1].anotherObj.myNumber = 11;
   
   abc[2] = new Object();
   abc[2].anotherObj = new Object();
   abc[2].anotherObj.myNumber = 2;
   
   abc[3] = new Object();
   abc[3].anotherObj = new Object();
   abc[3].anotherObj.myNumber = 3;
   
   abc.sortOn("anotherObj.myNumber", Array.NUMERIC);
   trace(abc[0].anotherObj.myNumber);
   trace(abc[1].anotherObj.myNumber);
   

```

In the above code… I want to sort on anotherObj.myNumber.  
The above doesn’t work.  
I wasn’t sure what I should put for anotherObj.myNumber??

Any help would be appreciated.

Thanks.

OM

---

<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:** [September 20, 2004, 3:00am UTC](https://forum.kirupa.com/t/how-can-i-sort-an-array-with-objects-in/64789/2 "2004-09-20T03:00:59Z")

</div>

look up Array.sortOn and/or sort functions (for Array.sort) in Flash help 🙂

---

<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:** [September 20, 2004, 5:52am UTC](https://forum.kirupa.com/t/how-can-i-sort-an-array-with-objects-in/64789/3 "2004-09-20T05:52:26Z")

</div>

Not sure this is the best way.

abc = new Array();  
reOrder = function (b, a) {  
var a = a.anotherObj.myNumber;  
var b = b.anotherObj.myNumber;  
if (a\<b) {  
return -1;  
} else if (a\>b) {  
return 1;  
} else {  
return 0;  
}  
};  
abc[0] = new Object();  
abc[0].anotherObj = new Object();  
abc[0].anotherObj.myNumber = 21;  
abc[1] = new Object();  
abc[1].anotherObj = new Object();  
abc[1].anotherObj.myNumber = 11;  
abc[2] = new Object();  
abc[2].anotherObj = new Object();  
abc[2].anotherObj.myNumber = 2;  
abc[3] = new Object();  
abc[3].anotherObj = new Object();  
abc[3].anotherObj.myNumber = 3;  
abc.sort(reOrder);
