Skip to content

Instantly share code, notes, and snippets.

@amowu
Created January 3, 2013 11:11
Show Gist options
  • Save amowu/4442706 to your computer and use it in GitHub Desktop.
Save amowu/4442706 to your computer and use it in GitHub Desktop.
ArrayCollection sort function.
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 Boolean descending
* @param Boolean numericSort
*
* @return ArrayCollection
*/
public static function sort(arrayCollection:ArrayCollection, sortBy:String, descending:Boolean=false, numericSort:Boolean=true):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 */
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment