Sorting AS3 numeric arrays?

var dataArray:Array = [ 5, 6, 3, 8, 10, 11, 32, 2 ]; var dataObjectArray:Array [ { "uid": 5 }, { "uid": 6 .... Similar to above ... } ]; 

I have 2 arrays in AS3. and I want to sort them numerically (1 ~ X) in the order [Skipping those that do not exist]. What is the best and most efficient way to do this for dataArray / dataObjectArray.

You can solve 1 or both =)

+4
source share
2 answers

You tried:

 dataArray.sort( Array.NUMERIC ); dataObjectArray.sortOn( ["uid"], [Array.NUMERIC]); 
+13
source

Surprisingly, as3 has built-in features for this.

  dataArray.sort(Array.NUMERIC); dataObjectArray.sortOn("uid", Array.NUMERIC); 

This will elegantly give the desired effect: Ascending by default. The array documentation covers additional information, such as the decreasing order of / etc ... Lol sortOn even sorts nested objects / array if field values ​​are specified.

+5
source

All Articles