SortOn an m-array of objects
Alright, so I have an m-array (Or an array of arrays in actionscript as it doesn't really have m-arrays) and each array in the superarray has a number of objects created at different times in it. I want to sort the superarray by in descending order of the value of the "time" paramater of the object at index 0 in each subarray.
I've tried
开发者_运维百科superarray.sortOn([0].time, Array.DESCENDING);
and
superarray.sortOn("0.time", Array.DESCENDING);
but this doesn't seem to work. Any suggestions? Will I just have to write my own sort function to do this? If so what's the best way to go about it?
Try using the Array.sort function passing a compare function. Something like this:
var superarray:Array = [
[{time:900}, {time:715}, {time:655}],
[{time:450}, {time:333}, {time:100}],
[{time:999}, {time:75}, {time:30}]
];
var sorted:Array = superarray.sort( function(A:Array,B:Array):int {
return ObjectUtil.numericCompare(A[0].time, B[0].time);
});
精彩评论