A Flash Developer Resource Site

# Thread: sub sorting multi-dimesional arrays

1. ## sub sorting multi-dimesional arrays

oh hai,

I have my 2D array right, and it kinda would look like this:

[
[250,350,0,11],
[250,250,0,12],
[250,225,1,2],
[250,175,1,1],
[300,150,0,5],
[400,10,0,7],
[400,250,0,8],
[450,350,0,10]
]

So as you can see I have already ordered my array by the first element in the sub arrays, what i need to do now is sub order said array so it will end up like:

[250,175,1,1],
[250,225,1,2],
[250,250,0,12],
[250,350,0,11],
[300,150,0,5],
[400,10,0,7],
[400,250,0,8],
[450,350,0,10]

Cheers

2. PHP Code:
``` for each(var sub:Array in outerArray){     sub.sort(Array.NUMERIC); }  ```

3. Kinda... but not what I want. That orders inside the sub-arrays numericaly I need to order the main array internally.

returns
Code:
```[0,11,250,350]
[0,12,250,250]
[1,2,225,250]
[1,1,175,250]
[0,5,150,300]
[0,7,10,400]
[0,8,250,400]
[0,10,350,450]```
instead of what I said above

4. It looks like you want one sort that compares a[1] to b[1] only if a[0] == b[0]. So what you really want is not a second sort, but a single sort that is more sensitive. Try this as the function to pass to sort:
Code:
```function compareByCol(a:Array, b:Array):int{
var i:int = 0;
while ((i < a.length) && (i < b.length)){
var diff:int = a[i] - b[i];
if (diff != 0){ //they differ
return diff; //return number representing whether a or b is less
}
i++;
}
//if we get here, we've exhausted at least one array.  treat the shorter one as lesser
return a.length - b.length
}```

5. AMAZING! thanks heaps! works perfectly XD

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap