Question

using Flash Builder 4.6 i want to sort an arrayCollection. the array has 2 properties status, and help_id. I want to sort the array to have all "open" statuses at the top, than all the "read", than "onsite", than "complete", and so on. i made a function that does this but i want all the items with the same status to than be sorted by the help_id property highest first low est last.

this is my code to sort the statuses.

    [Bindable]protected var myHelp:ArrayCollection = new ArrayCollection();

Sort Function:

    function sortFunction(a:Object, b:Object, array:Array = null):int
                {

                    var status:Array = ["open", "read", "onsite","complete", "reopen", "closed"];
                    var aStatus:Number = status.indexOf(a.status);
                    var bStatus:Number = status.indexOf(b.status);
                    if(aStatus == -1 || bStatus == -1)
                        throw new Error("Invalid value for criticality ");
                    if(aStatus == bStatus)
                        return 0;
                    if(aStatus > bStatus)
                        return 1;
                    return -1;
                }
                var sort:Sort = new Sort();
                sort.compareFunction = sortFunction;
                myHelp.sort = sort;
                myHelp.refresh();

any Help would be much appreciated.

Was it helpful?

Solution

instead of looking for string index in an array, why not use ints for both. In your code, you could reference constants like StatusType.OPEN etc... then your code could look like:

var sort:Sort = new Sort();
sort.fields = [new SortField("status", true, true), new SortField("help_id", true, true)];
myHelp.sort = sort;
myHelp.refresh();

This would sort by status then help_id at the same time. Then when you need to display the status to a user in GUI, convert the int to a String human readable value

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top