# Sorting multidimensional array

**URL:** <https://forum.kirupa.com/t/sorting-multidimensional-array/111316>\
**Category:** Uncategorized\
**Created:** [May 25, 2004, 8:24pm UTC](https://forum.kirupa.com/t/sorting-multidimensional-array/111316 "2004-05-25T20:24:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Boondogger](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@Boondogger](https://forum.kirupa.com/u/Boondogger)\
**Post date:** [May 25, 2004, 8:24pm UTC](https://forum.kirupa.com/t/sorting-multidimensional-array/111316/1 "2004-05-25T20:24:06Z")

</div>

I have an array of objects (the first member of each object is a string which records the object’s name) The other members of the objects contain various numerical information. I need to sort each category of information and display the names next to their related information in correct numerical order:  
ie. AName 4235  
AnotherOne 3962  
OneMore 1576

Can anyone suggest some code which would do this? I checked out .sort and .onSort, built in methods of array but I can’t get my head round it.There are thirteen objects in my array. The fourth element (population) needs to be sorted. I tried  
x=0;  
for (i=0;i\<14;i++) {//counts through objects in array  
if ( EmpireList \* [3] \>x)  
x= EmpireList \* [3];//loops through array to find greatest numerical value  
} //and writes it into x  
LargestInArray = x;

This gave me the biggest numerical element in the array. I figured I needed some kind of nested for loop to find the next largest and so on but I can’t get it to work.

Ideally I need to copy the information into a new array with two dimensions (for name and information)

Thanks

MD

---

<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:** [May 25, 2004, 8:32pm UTC](https://forum.kirupa.com/t/sorting-multidimensional-array/111316/2 "2004-05-25T20:32:22Z")

</div>

arrays have various built-in sort functions. check them out 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:** [May 25, 2004, 9:20pm UTC](https://forum.kirupa.com/t/sorting-multidimensional-array/111316/3 "2004-05-25T21:20:49Z")

</div>

```auto

// simulation of your array of objects
ary = [{name:"sheep", id:101},
	 {name:"elephants", id:56},			 
	 {name:"aardvarks", id:300}];

// sort by the field 'id'
ary.sort( function(a,b) { return a.id - b.id; } );

// display the results
for (var i = 0; i < ary.length; ++i) {
	trace(ary*.id + ": " + ary*.name);
}

```

Output:

56: elephants  
101: sheep  
300: aardvarks

Note to admin: The [as] tag doesn’t handle indents very well…

---

<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:** [May 26, 2004, 11:33am UTC](https://forum.kirupa.com/t/sorting-multidimensional-array/111316/4 "2004-05-26T11:33:02Z")

</div>

Yep. That did the trick. Thanks a lot. C:-)
