Sorting Arrays

Hey everyone,

Got a tricky issue sorting arrays of objects. I have an array (Data) that contains several Array’s of objects. For example Data[0] is a list of objects of type “Program”. Data[1] is a list of type “Series”, etc. I was trying to make a single catch all sorting function so I don’t have to hand write essentially the same code for each property on all the different classes.

I have set up two functions to sort the arrays that work with one minor exception. I need to pass a variable to my sort function telling it which property to use for the sort. I’ve tried defining it in the scope of the class but, in the function, it is “undefined”. I’m betting this is just a simple scoping issue but, sadly, I’ve forgotten way too much about AS2 since I started writing AS3 to remember a way around it.

Here is an abbreviated version of the two functions within the class:


private var CurrentSortProperty    :String;

public function getSorted(list:Number, sortOn:String):Array
{
    CurrentSortProperty = sortOn;
    return Data
[list].sort(order);
}
    
private function order(a, b):Number
{
    if (a[CurrentSortProperty ] < b[CurrentSortProperty ]) {
        return -1;
    } else if (a[CurrentSortProperty ] > b[CurrentSortProperty ]) {
        return 1;
    } else {
        return 0;
    }        
}

If anyone has any suggestions how I can get access to “CurrentSortProperty” in the class, I would appreciate it.

Thanks!

Steve