mardi 20 décembre 2016

Value specified in SortField not being used by ArrayCollection Sort

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.


<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ift.tt/rYUnH9" 
                       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>

Aucun commentaire:

Enregistrer un commentaire