User Guide

252 Working with Arrays
Sorting by property and specifying sort options
The work of actually sorting the list of songs is performed by the PlayList class in the
sortList() method, as follows:
/**
* Sorts the list of songs according to the specified property.
*/
public function sortList(sortProperty:SortProperty):void
{
...
var sortOptions:uint;
switch (sortProperty)
{
case SortProperty.TITLE:
sortOptions = Array.CASEINSENSITIVE;
break;
case SortProperty.ARTIST:
sortOptions = Array.CASEINSENSITIVE;
break;
case SortProperty.YEAR:
sortOptions = Array.NUMERIC;
break;
}
// Perform the actual sorting of the data.
this._songs.sortOn(sortProperty.propertyName, sortOptions);
// Save the current sort property.
this._currentSort = sortProperty;
// Record that the list is sorted.
this._needToSort = false;
}
When sorting by title or artist, it makes sense to sort alphabetically, but when sorting by year,
it’s most logical to perform a numeric sort. The
switch statement is used to define the
appropriate sorting option, stored in the variable
sortOptions, according to the value
specified in the
sortProperty parameter. Here again the named enumeration members are
used to distinguish between properties, rather than hard-coded values.
With the sort property and sort options determined, the
_songs array is actually sorted by
calling its
sortOn() method, passing those two values as parameters. The current sort
property is recorded, as is the fact that the song list is currently sorted.