kbgn
kbgn

Reputation: 856

Sorting a Flex Array

I have created an array as shown below

protected function getMyArray(dataArray:Array):Array
    {
        var labelList:Array=new Array;
        for each (var property:Object in dataArray)
        {
            if (labelList[property.bucketTime] != property.bucketTime)
                labelList[property.bucketTime]=property.bucketTime;
        }
        return labelList;
    }

Is it possible to sort the array labelList based on property.bucketTime?

Edit: Sample input dataArray will be like this :

var tempObj:Object = new Object;
            tempObj.bucketTime = DateField.stringToDate("30-01-2010", "DD-MM-YYYY").time;
            tempObj.score = 76;
            dataArray.addItem(tempObj);

            tempObj = new Object;
            tempObj.bucketTime = DateField.stringToDate("13-02-2010", "DD-MM-YYYY").time;
            tempObj.score = 21;
            dataArray.addItem(tempObj);

            tempObj = new Object;
            tempObj.bucketTime = DateField.stringToDate("30-03-2010", "DD-MM-YYYY").time;
            tempObj.score = 10;
            tempArry.addItem(tempObj);

Upvotes: 1

Views: 2438

Answers (2)

kbgn
kbgn

Reputation: 856

Atlast I sorted the labelList Array.Please find my solution below. Let me know if there is any better way to achieve this.

var termObject:Object = ObjectUtil.getClassInfo(labelList);
        var termsArray:Array = termObject.properties;
        var sortedColumnArray:Array = new Array;
        for each(var term:Object in termsArray){
            if(labelList.hasOwnProperty(term)){
                sortedColumnArray.push(labelList[term]);
            }
        }

Upvotes: 0

JeffryHouser
JeffryHouser

Reputation: 39408

Unless bucketTime is a number; then you aren't actually populating the array. You're just adding properties to the Array Object, almost like it were a Dictionary. I've seen a Dictionary called Associative Array's and Structures in other languages.

If that is the case, an you're using the Array class as a dictionary, then there is no way to sort it. The very nature of such a structure is that they are not sortable.

However, if property.bucketTime is a number, and you are trying adding items to the array as if they were an array, you can sort using the Array.sort or Array.sortOn methods.

Upvotes: 3

Related Questions