I have a list of items in an ArrayCollection that I'd like sorted by a certain value (not alphabetical). Is it possible to sort this way?
Here are the values I want to be ordered by:
Resolved
Closed
Open
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In Addition to the provided answer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If your array collection is part of a DataGrid then you can set the sortCompareFunction on the DataGrid's GridColumn. Note that the syntax is different for this sort compare function as the grid colummn is passed in rather than the fields.
EXAMPLE CODE
used with a Spark DataGrid
private var desiredItemOrder:Array = ["In Progress", "Open", "Resolved", "Closed"];
private function sortCompareFunction(item:Object, anotherItem:Object, column:GridColumn = null):int
{
var itemIndex:Number = desiredItemOrder.indexOf(item.fields.status.name);
var anotherItemIndex:Number = desiredItemOrder.indexOf(anotherItem.fields.status.name);
if (itemIndex == -1 || anotherItemIndex == -1)
throw new Error("Invalid value for criticality ");
if (itemIndex == anotherItemIndex)
return 0;
if (itemIndex > anotherItemIndex)
return 1;
return -1;
}
<s:DataGrid dataProvider="{issuesCollection}">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="fields.status.name" headerText="Status" sortCompareFunction="sortCompareFunction"/>
</s:ArrayList>
</s:columns>
</s:DataGrid>
DOCUMENATION
Sort compareFunction
DataGridColumn sortCompareFunction
GridColumn sortCompareFunction