Reputation: 12712
After applying a numeric sort to my dataprovider(Array Collection), I can not reorder the items via a tilelist. Do I need to remove the sort from the arrayCollection. If so, is it just a case of setting collection.sort = null ?
var sortField:SortField=new SortField();
sortField.name="order";
sortField.numeric=true;
var sort:Sort=new Sort();
sort.fields=[sortField];
Upvotes: 3
Views: 4255
Reputation: 11
Adobe Flex - Sorting an ArrayCollection by Date
/** * @params data:Array * @return dataCollection:Array **/ private function orderByPeriod(data:Array):Array { var dataCollection:ArrayCollection = new ArrayCollection(data);//Convert Array to ArrayCollection to perform sort function var dataSortField:SortField = new SortField(); dataSortField.name = "period"; //Assign the sort field to the field that holds the date string var numericDataSort:Sort = new Sort(); numericDataSort.fields = [dataSortField]; dataCollection.sort = numericDataSort; dataCollection.refresh(); return dataCollection.toArray(); }
Upvotes: 1
Reputation: 13994
I got caught with this problem too, I found your question, and I still didnt get it solved like Christophe suggested.
After suffering with this for a while, I discovered one way to avoid the problems you mentioned.
Simply use an auxiliary ArrayCollection to do the sort. Anyway your Sort instance seems to be temporary (you want to through it away), so why not use a temporary ArrayCollection?
Here's how my code looked like:
// myArrayCollection is the one to sort
// Create the sorter
var alphabeticSort:ISort = new Sort();
var sortfieldFirstName:ISortField = new SortField("firstName",true);
var sortfieldLastName:ISortField = new SortField("lastName",true);
alphabeticSort.fields = [sortfieldFirstName, sortfieldLastName];
// Copy myArrayCollection to aux
var aux:ArrayCollection = new ArrayCollection();
while (myArrayCollection.length > 0) {
aux.addItem(myArrayCollection.removeItemAt(0));
}
// Sort the aux
var previousSort:ISort = aux.sort;
aux.sort = alphabeticSort;
aux.refresh();
aux.sort = previousSort;
// Copy aux to myArrayCollection
var auxLength:int = aux.length;
while (auxLength > 0) {
myArrayCollection.addItemAt(aux.removeItemAt(auxLength - 1), 0);
auxLength--;
}
It's not the neatest code, it has some weird hacks like auxLength instead of aux.length (this one gave me -1 array range exception), but at least it solved my problem.
Upvotes: 1
Reputation: 16085
Setting the sort to null should indeed remove the sort for the collection. You might need to do an optional refresh().
Upvotes: 4