I have an array collection:
private var arrayCollection:ArrayCollection = new ArrayCollection([
{Type:L, Name:"DDD"},
{Type:H, Name:"EEE"},
{Type:H, Name:"AAA"},
{Type:L, Name:"AFG"},
{Type:L, Name:"ZZZ"},
{Type:H, Name:"XXX"},
{Type:, Name:"DeSelectAll"},
{Type:G, Name:"NNN"},
{Type:L, Name:"CCC"},
{Type:, Name:"SelectAll"},
{Type:H, Name:"BBB"},
{Type:H, Name:"HHH"},
{Type:L, Name:"LLL"}
]);
I want to sort this array collection using these two fields Type and Name. First, all record will come of Type "H" with Name sort after that All record will come of Type "L" with Name sort And after that Type of "G" and "DeSelectAll" at the top of the list and "SelectAll" after the "DeSelectAll". I want the result like this.
Name:"DeSelectAll" Type:""
Name:"SelectAll" Type:""
Name:AAA Type: H
Name:BBB Type: H
Name:EEE Type: H
Name:HHH Type: H
Name:XXX Type: H
Name:AFG Type: L
Name:CCC Type: L
Name:DDD Type: L
Name:LLL Type: L
Name:ZZZ Type: L
Name:NNN Type: G
Please provide some code for that using sort().
So basically you want to sort on Type and then on Name, should work with something like this:
var nameSortField:SortField = new SortField("Name");
var typeSortField:SortField = new SortField("Type");
arrayCollection.sort = new Sort();
arrayCollection.sort.fields = [typeSortField, nameSortField];
arrayCollection.refresh();
EDIT:
If you need a custom order for the Type property (so "G" comes at the end) you would need a custom compare function for the Type field:
// This is the sort order for your Type. First empty, then H, then L and then G
// It is not clear if G, H and L are properties in your code. If that are properties you should remove the quotes around G, H and L
private var typeOrder:Array = ["","H", "L", "G"];
var nameSortField:SortField = new SortField("Name");
var typeSortField:SortField = new SortField("Type");
typeSortField.compareFunction = typeCompareFunction;
arrayCollection.sort = new Sort();
arrayCollection.sort.fields = [typeSortField, nameSortField]
arrayCollection.refresh();
private function typeCompareFunction(a:Object, b:Object, fields:Array = null):int
{
if (typeOrder.indexOf(a.Type) < typeOrder.indexOf(b.Type))
{
// if the a.Type position in the array is before b.Type position the item a should be before b
return -1;
}
else if (typeOrder.indexOf(a.Type) > typeOrder.indexOf(b.Type))
{
// if the a.Type position in the array is after b.Type position the item a should be after b
return 1;
}
else
{
return 0;
}
}