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:
- Create a Sort object
- Create one or more SortField objects
- Set the Sort/Fields on the Collection
- 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;

