Today we have a strange AS3 array sort problem, and we know that Sorton can sort the elements of a group by a specified field. I do in the mail system, first according to the message status row, and then according to the index row
But the planning and I said there are bugs, I see there are really, all messed up. So I checked, found that the server is not wrong, should be sorton sort of wrong.
Test program:
The code is as follows |
Copy Code |
1.var arr:array = []; 2.var Mailvo:object; 3.for (var i:int = 0;i < i++) 6.9 5. Mailvo = new Object (); 6. Mailvo.index = Index of i;//message, first sent index = 0 7. mailvo.state = 1;//message status, ==0 is unread, ==1 is read 8. Arr.push (Mailvo); 7.3 10.//I want unread messages to appear before 11.arr.sorton (["State"],[array.numeric]); 12. 13.for (i = 0;i < i++) 14.{ Trace (Arr[i].index); 16.}
|
Output results:
10 1 2 3 4 5 6 7 8 9 0 11 12 13 14 15 16 17 18-19
I was thinking that if state is the same, the order should not change Ah!
Through group help, I knew the problem. The solution is:
Arr.sorton (["State"],[array.numeric | Array.uniquesort]);
Uniquesort: If the array has the same element, the sort is discarded
That's what the handbook says:
If any of the two elements or fields are to be sorted with the same value, the unique sort option terminates the sort.
And the sort of thing to note:
The element is sorted as a string regardless of the data type, so 100 is before 99, because the string value of "1" is less than the string value of "9".