Follow Flex After Dark on Twitter

Collection Sorting in Flex

Collection sorting is programmed using a Sort object and one or more SortField objects from the mx.collections package.

General steps for setting up collection sorting:

  1. Create a Sort object
  2. Create one or more SortField objects
  3. Set the Sort/Fields on the Collection
  4. Call the Collection's refresh() method
   collection.sort = new Sort();
   collection.sort.fields = [ new SortField(null); ];
   collection.refresh();

Sort Fields

Sort Fields are used by a Sort object to sort the items in the collection by one or more of each item's field. A SortField object encapsulates what field to sort by and how to sort it.

So each SortField added to a sort corresponds to a field of the collection items.

For instance, a sort field could specify to sort by items by the "firstName" field case-insensitively.

   // create a case-insensitive sort field
   var sortField1:SortField = new SortField( "firstName" );
   sortField1.caseInsensitive = true;

As another example, a sort field could specify to sort the "pageCount" field, a numeric value, in descending order.

   // create a numeric, descending sort field
   var sortField2:SortField = new SortField( "pageCount" );
   sortField2.numeric = true;
   sortField2.descending = true;

Sort fields are then applied to a Sort object as the fields property (array).

   var sort:Sort = new Sort();
   sort.fields = [ sortField1, sortField2 ];

If the items in the collection are all simple objects (e.g. Strings, Numbers or Dates) then a SortField instance is created passing null for the fieldName.

   // create a sort field for simple item values (no field name)
   var sortField:SortField = new SortField( null );
   sortField1.descending = true;

Related Links:

Related Docs
  • ActionScript ArrayCollection The ArrayCollection class is a robust collection class that can be more useful than a standard array.
  • Flex Collections Collections are objects that provide a uniform way to access and represent item lists.
Recent Docs
Random Docs
  • ActionScript ExternalInterface ActionScript's ExternalInterface class and IExternalInterface interface allow for two-way communication between ActionScript and browser JavaScript.
  • Flex Builder Overview Overview of FlexBuilder, Adobe's integrated development environment (IDE) for Flex development.
  • Flex Date Controls Flex Date Controls allow users to select dates and date ranges with DateField and DateChooser.
  • ActionScript Exceptions ActionScript handles Exceptions using try, catch, and finally.
  • ActionScript Bindable ActionScript supports binding via the [Bindable] metadata tag.