Sort an ArrayCollection in Flex

Posted on August 12, 2010
by

As nothing’s as straight forward as PHP, I had to write my own class to sort ArrayCollections. My work was inspired by what Peter Dehaan did.

The method is to be used as follows:

ArrayUtils.sort(games, "playerScore", "DESC", true);

to sort an ArrayCollection of games by player score, with a descending order. The last parameter indicates that we want a numeric sorting rather than a alphabetical sorting.

I encapsulated the method in an ArrayUtils class, but you’re free to do as you wish. That’s because I think I will add more methods to my class in a near future. You can download the class here: ArrayUtils.as

package classes.utils
{
import mx.collections.ArrayCollection;
import mx.collections.Sort;
import mx.collections.SortField;
 
/**
* Useful functions to manipulate arrays
*
* @author Cyril Mazur cyrilmazur.com
*/
public class ArrayUtils
{
/**
* Sort an arrayCollection by key
* order MUST be either "ASC" or "DESC"
*
* @author Cyril Mazur cyrilmazur.com
* @see http://blog.flexexamples.com/2007/08/05/sorting-an-arraycollection-
using-the-sortfield-and-sort-classes/
*
* @param ArrayCollection arrayCollection
* @param String sortBy
* @param String order
* @param Boolean numericSort
*
* @return ArrayCollection
*/
public static function sort(arrayCollection:ArrayCollection, sortBy:String,
order:String, numericSort:Boolean):ArrayCollection {
/* Create the sort field and fill it */
var dataSortField:SortField = new SortField();
dataSortField.name = sortBy;
dataSortField.caseInsensitive = true;
dataSortField.numeric = numericSort;
 
/* Set the order, by default it's ascending */
if (order.toUpperCase() == "DESC") {
dataSortField.descending = true;
}
 
/* Create the Sort object and add the SortField object
created earlier to the array of fields to sort on. */
var dataSort:Sort = new Sort();
dataSort.fields = [dataSortField];
 
/* Set the ArrayCollection object's sort property to our
custom sort, and refresh the ArrayCollection. */
arrayCollection.sort = dataSort;
arrayCollection.refresh();
 
/* Return the collection */
return arrayCollection;
}
}
}

Download the source here: ArrayUtils.as

I’m open to any suggestion to improve the code 🙂

About the author

Cyril Mazur is a serial web entrepreneur with experience in various fields: online dating, forex & finance, blogging, online advertising... who enjoys building things that people like to use.

4 comments

  1. Cal
    on September 2, 2011
    Let's say you have an ArrayCollection called dates that is used as a data provider for a listbox. When you add dates to the listbox you want the listbox to automatically sort the dates. You can do this with the sort function. Here's the basics...

    import mx.collections.Sort;
    import mx.collections.SortField;
    import mx.collections.IViewCursor;

    private var cursor:IViewCursor;

    // Sort
    var sort:Sort = new Sort();
    sort.fields = [new SortField("booking_date")]
    dates.sort = sort;
    dates.refresh();

    // Optionally create a view cursor if you want to later find specific data in the provider
    cursor = dates.createCursor();

    Simple, no?
  2. Cyril
    on September 5, 2011
    Hi Cal,

    I had no idea that the class Sort existed, that is a good thing to point out, thank you!
  3. Bashu
    on June 26, 2012
    Its really a simple example. And working fine. Thanks.
  4. Cyril
    on July 2, 2012
    Thanks Bashu

Leave a Reply