-1

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.

user1535213
  • 5
  • 1
  • 6
  • If you could tell us what your problem was; and why your approach wasn't working we'd be better suited to help you solve it. – JeffryHouser Nov 02 '12 at 20:20
  • what i showed you works perfect. i just want to do a second sort on the statuses that are the same. like if there is 2 statuses the are closed i want to have the one with the higher help_id to be displayed first.(the myHelp arrarCollection has 2 pats to it, myHelp.status(string) and myHelp.help_id(int). – user1535213 Nov 02 '12 at 20:59

1 Answers1

0

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

Jason Reeves
  • 1,716
  • 1
  • 10
  • 13