Wednesday, March 23, 2011

Flex - ActionScript: how do I use SortField to sort my data numericly

From the documentation:

SortField () constructor

public function SortField(name:String = null, caseInsensitive:Boolean = false, descending:Boolean = false, numeric:Object = null)

I'm confused on the last part - numeric:Object = null

Here is my function:

private function createXMLDataProvider():XMLListCollection{    
       var sort:Sort  = new Sort();
        sort.fields = [new SortField("@sortorder",true,false,true), new SortField("@label")];
       var searchTypesCollection:XMLListCollection = new XMLListCollection(getAssociations(_appData.searchTypes, "category", searchType));
        searchTypesCollection.sort = sort;
        searchTypesCollection.refresh();
      return  searchTypesCollection;
      }

On this line:

sort.fields = [new SortField("@sortorder",true,false,true), new SortField("@label")];

The first SortField is a number but is being compared like it is text, what should I be putting where it says true?

Also from the documentation:

Specifies that if the field being sorted contains numeric (number/int/uint) values, or string representations of numeric values, the comparitor use a numeric comparison. If this property is false , fields with string representations of numbers are sorted using strings comparison, so 100 precedes 99, because "1" is a lower string value than "9". If this property is null , the first data item is introspected to see if it is a number or string and the sort proceeds based on that introspection

The default value is false.

From stackoverflow
  • Have a look here for an example of sorting an XMLListCollection:

    http://blog.flexexamples.com/2007/08/05/sorting-an-arraycollection-using-the-sortfield-and-sort-classes/

    If you scroll down to Vivek's comment, there's an example and a follow-up comment from Peter deHaan. It looks like he's just omitting the caseInsensitive property, rather than setting it explicitly in the constructor, e.g.:

    var sortField:SortField = new SortField(value);
    sortField.numeric = true;
    sortField.descending = true;
    
    var sort:Sort = new Sort();
    sort.fields = [sortField];
    xmlListColl.sort = sort;
    return xmlListColl.refresh();
    

    Hope that helps! Incidentally, true is correct for the numeric property -- I'm not sure why it accepts Object; it looks like in Flex 2, it was a Boolean, and in Gumbo, it's still an Object, although the following line's been added to the Gumbo docs:

    When this property is modified, it dispatches the numericChanged event.

    A clue, perhaps? Nevertheless, a Boolean value is fine, yes.

  • I'm just guessing here, but caseInsensitive and numeric fields seem like an either-or choice to me, i.e. if you set caseInsensitive to true, there is no real point in also setting numeric, and vice versa. Could you try modifying the above line as follows:

    sort.fields = [new SortField("@sortorder",false,false,true), new SortField("@label")];
    

    and see if it works that way?

0 comments:

Post a Comment