Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
527 views
in Technique[技术] by (71.8m points)

flash - AS3 - Sorting an array of nested arrays

How would I go about sorting an array of nested arrays, based on the contents of one of the nested arrays elements?

var nestedArray1:Array = new Array(0,0,1);
var nestedArray2:Array = new Array(0,0,9);
var nestedArray3:Array = new Array(0,0,7);
var nestedArray4:Array = new Array(0,0,3);

var parentArray:Array = new Array(nestedArray1,nestedArray2,nestedArray3,nestedArray4);

I would like to end up with an array sorted using the nested arrays 3rd element.

So tracing sortedParentArray would give me: 0,0,1,0,0,3,0,0,7,0,0,9

which is nestedArray1, nestedArray4, nestedArray3, nestedArray2

Thanks,

Mark

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since you can access an array index in the same way as a property (array[2] is the same as array["2"]) you can use sortOn.

parentArray.sortOn("2", Array.NUMERIC);

You can also use the other indices as second or third sort fields if don't want an unpredictable order for equal entries.

parentArray.sortOn(["2","1","0"], Array.NUMERIC);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...