Reputation: 310
I have had a break of AS3 for a while and I'm rusty to say the least.
I would like my array to be sorted in size order (smallest to biggest).
this is my code
public function sort_by_value():void
{
var tmp_array:Array = new Array();
var counter:Number = 1;
for each (var hand_card:Number in TABLE.CARDS_IN_PLAYER_1_HAND)
{
tmp_array.push(hand_card);
trace(hand_card)
counter++;
}
tmp_array.sortOn(Array.DESCENDING);
trace(tmp_array);
}
This is the output.
34
40
51
30
8
27
14
52
36
19
50
33
9
14,40,51,30,8,27,34,52,36,19,50,33,9
Why isn't it sorting corrently?
Upvotes: 0
Views: 182
Reputation: 60527
That's because sortOn
is used to sort objects by a property (documentation link).
sortOn()
Allows you to sort objects that have one or more common properties, specifying the property or properties to use as the sort keys
Use sort
instead.
sort()
Allows you to sort the Array’s elements in a variety of predefined ways, such as alphabetical or numeric order. You can also specify a custom sorting algorithm.
You will also want to use the Array.NUMERIC
flag.
tmp_array.sort(Array.DESCENDING | Array.NUMERIC);
Upvotes: 1