1

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

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
  • 1
    possible duplicate of [Sorting an ArrayCollection in Flex](http://stackoverflow.com/questions/1602385/sorting-an-arraycollection-in-flex) – JeffryHouser Sep 04 '12 at 21:32
  • Short answer is to use a sortCompareFunction; for longer answer look at the other question; or at these docs: http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf668d2-7ff2.html#WS2db454920e96a9e51e63e3d11c0bf69084-7b66 – JeffryHouser Sep 04 '12 at 21:32
  • Thanks. The related question helped. BTW I didn't see anything on sortCompareFunction on the page you linked but I found something on the compareFunction on a link on that page http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/spark/collections/Sort.html#compareFunction. – 1.21 gigawatts Sep 05 '12 at 02:13
  • Regarding the duplicate issue, that question provides a solution with no further information but that alone didn't give me the details I was looking for. I appreciate the link to the documentation you provided which did. – 1.21 gigawatts Sep 05 '12 at 02:16

2 Answers2

2

this may be of some help to you

Sorting an ArrayCollection in Flex

Community
  • 1
  • 1
S.Ali
  • 664
  • 1
  • 5
  • 12
1

If you want to sort only 3 words - make this comparison in sort function like:

private static var valuesOrder : Array = ["Resolved", "Closed", "Open"];
private function sortFunction(a:Object, b:Object, array:Array = null) : int{
    var indexA : int = valuesOrder.indexOf(a);
    var indexB : int = valuesOrder.indexOf(b);
    return indexA - indexB;//return positive if indexA > indexB
}
var sort : Sort = new Sort();
sort.compareFunction = sortFunction;
arrayCollection.sort = sort;
arrayCollection.refresh();

Note, if value not exists in valuesOrder array - index will be -1. Process this situation as you wish.

radistao
  • 14,889
  • 11
  • 66
  • 92