leif
leif

Reputation: 195

How do I use SortField to sort my data numerically in Flex/ActionScript?

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 comparator 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.

Upvotes: 2

Views: 8032

Answers (2)

David Hanak
David Hanak

Reputation: 10974

I'm just guessing here, but caseInsensitive and numeric fields seem like an either-or choice to me, that is, if you set caseInsensitive to true, there is no real point in also setting numeric, and vice versa. Try modifying the above line as follows and see if it works that way.

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

Upvotes: 0

Christian Nunciato
Christian Nunciato

Reputation: 10409

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.

Upvotes: 1

Related Questions