Value specified in SortField not being used by ArrayCollection Sort

68 Views Asked by At

I saw this earlier question (Flex arraycollection sorting not working) but it doesn't seem to pertain to the issue I am seeing.

I am trying to do a sort of an ArrayCollection using a custom compare function (using the example from here: Alphanumeric Sorting in AS3 )

The problem: It seems that the field name specified in my SortField instance is not being passed to the compare function – instead the objects themselves are. This seems wrong – but maybe I am misunderstanding (this question and the answers are a little confusing: Flex: Sort -- Writing a custom compareFunction?) – what is the point of specifying a field name in the SortField if it isn't going to be used?

A stripped down example is below. I am trying to sort a list of File instances. A trace statement in the compare function confirms that the File instance, not their name properties are being passed as arguments.

Do I need to customize the compare function? That is ** cough ** less that an optimal solution for reusable coding.


Update:

As often happens, I come up with a solution withing 10 minutes of posting a question. In this case using a proxy function. Still, I am wondering why the value specified in SortField isn't being used.

    public function customCompare(obj1:*, obj2:*):int
    {
        return AlphaNumericSort.compare(obj1.name, obj2.name);
    }

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       creationComplete="windowedapplication1_creationCompleteHandler(event)">

    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.events.FlexEvent;

            import spark.collections.Sort;
            import spark.collections.SortField;

            import utils.AlphaNumericSort;

            protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
            {
                var directory:File = File.desktopDirectory.resolvePath("testFolder");

                if (directory.exists){

                    var collection:ArrayCollection;     
                    var obj:*;
                    var sort:Sort;
                    var sortField:SortField;

                    collection = new ArrayCollection(directory.getDirectoryListing());
                    sort = new Sort();
                    sortField = new SortField("name");
                    sortField.compareFunction = AlphaNumericSort.compare;
                    sort.fields = [sortField];
                    collection.sort = sort;
                    collection.refresh();

                    trace("---------AlphaNumericSort")
                    for each (obj in collection){
                        trace(obj.name);
                    }
                    trace(" ");
                }
            }

        ]]>
    </fx:Script>
</s:WindowedApplication>
1

There are 1 best solutions below

0
On

I have tested your code. It works fine. Only, in your code replace the below line sortField.compareFunction = AlphaNumericSort.compare;
to sortField.compareFunction = customCompare;