derChris
derChris

Reputation: 890

Flex Sort is sorting where nothing should be sorted (in an ArrayCollection)

I try to sort an arrayCollection with this Sort:

    private function sortArray(questions:ArrayCollection):void
    {
        questions.sort = new Sort();
        questions.sort.fields = [new SortField("rank")];
        questions.sort.compareFunction = rankFunction;
        questions.refresh();
    }

    private function rankFunction(a:int, b:int, array:Array = null):int
    {
        if(a == b)
        {
            return 0;
        }
        if(a>b)
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }

There are 23 Objects and all have the rank = 0

I expected that nothing will be changed but after the refresh the items at postion 0 and 11 of the ArrayCollection swapped their position.

In the rankFunction, there is always returned 0.

Can anyone tell me what is going wrong here?

Upvotes: 1

Views: 59

Answers (1)

Denis Kokorin
Denis Kokorin

Reputation: 895

ASDoc says that compare function should have following signature:

function [name](a:Object, b:Object, fields:Array = null):int

and that the fields array specifies the object fields.

So Sort doesn't extract field values for you. You should do it yourself.

Concerning your question, it seems that AS3 implicitly converts Objects to ints invoking rankFunction. If so items are compared by some internal code that somehow corresponds to the order of items creation.

Upvotes: 1

Related Questions