AlBirdie
AlBirdie

Reputation: 2071

Stop ArrayCollection from being constantly sorted (sort only once)

I've got a list propagated by an ArrayCollection that holds instances of a model class. This model class has a class reference to a dictionary that holds stock values. Naturally these values get refreshed constantly.

When I apply my sort function to the ArrayCollection I do get the correct sorting, however the sort function constantly runs, I need it to run only once, though. So the sort should stop immediately after sorting the ArrayCollection for the first time.

My sort function is triggered on click of a header button (I'm working on a list based DataGrid optimized for mobile so constantly sorting the ArrayCollection is not only not required, it also requires too much performance) and looks like the following:

private function headerClick(event:MouseEvent):void {

    sField = event.currentTarget.id;        
    var sort:Sort = new Sort();
    sort.compareFunction = fidSort;

    (_list.dataProvider as ArrayCollection).sort = sort;
    (_list.dataProvider as ArrayCollection).refresh();
}

private function fidSort(a:Object, b:Object, fields:Array = null):int {

    if(a.fidList.fidMap[sField].fieldValue == b.fidList.fidMap[sField].fieldValue) {
        return 0;
    } else if(a.fidList.fidMap[sField].fieldValue > b.fidList.fidMap[sField].fieldValue) {
       return 1;
    } else{
       return -1;
    }
}

So, is there a way to stop the sorting process other than simply putting a boolean value at the end of the function to stop the sorting? That would mean that the function would still get dispatched with every update of the values, a rather undesired behavior.

Upvotes: 0

Views: 1247

Answers (1)

Sunil D.
Sunil D.

Reputation: 18194

Al_Birdy is right, this is the default behavior of a ListCollectionView (which ArrayCollection extends). ListCollectionView defines a method named disableAutoUpdate() which will prevent CollectionChange and PropertyChange events from being dispatched by the collection when items change.

This should then stop triggering your sort. It's still not ideal, b/c the collection apparently keeps track of all the changes when you call this method, so it can replay them if you later call enableAutoUpdate().

To work around this, you may just want to sort the data yourself, then create an ArrayCollection with the sorted data.

Upvotes: 2

Related Questions