2 Replies Latest reply on Sep 8, 2007 9:32 PM by jfillman

Bug in Array Sorting???

Here is my code:

for(var pNum:int=0;pNum<=144;pNum++){
var itemCount:Number = this[("P"+pNum)].length;
var left:Number = 58;
this[("P"+pNum)].source.sortOn("height", Array.DESCENDING);
for (var theArray:int=0;theArray<this[("P"+pNum)].length;theArray++){
}

If I have two items in my array, one with height of 200, and one with height of 80. The sort puts 80 ahead of 200. If my item heights are 80 and 40, then it sorts correctly and puts the 80 ahead of the 40. If my item heights are 140 and 100, then it sorts it wrong, putting 100 ahead of 140. The sorting only works correctly for numbers under 100. Is this a bug, a limitation, or am I doing something wrong?
• 1. Bug in Array Sorting???
Well, first looking at that block of code, I'd say you might benefit from a little refactoring to simplify whatever you're doing.

for(var pNum:int=0 ; pNum <= 144 ; pNum++){

sortByHeight(pNum);
}

private function sortByHeight(index:int):void {

var itemCount:Number = this[index].length;
var left:Number = 58;
this[index].source.sortOn("height", Array.DESCENDING);

for (var theArray:int=0; theArray < this[index].length ; theArray++){
}
}

You may just need to do this instead of just "Array.DESCENDING", it sounds like it's sorting your numbers as ASCII characters or something:

Array.DESCENDING | Array.NUMERIC
• 2. Re: Bug in Array Sorting???
I've spent weeks on this code. I'm working on something quite complex. My first pass through had hundreds of if statements, which became 3,000 + lines.

This "Array.DESCENDING | Array.NUMERIC" resolved it. Thank you.