Removing ArrayCollection

After applying numerical sorting to my dataprovider (Array Collection), I cannot reorder the items through the tilelist. I need to remove sorting from an array. If so, is this 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];
+3
source share
3 answers

Setting the sort to null should really remove the sort for the collection. You may need to do an optional update ().

+4
source

Source

Adobe Flex - Sort ArrayCollection array 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();
}
+1
source

, , , .

After suffering this for a while, I discovered one way to avoid the problems you were talking about.

Just use the ArrayCollection helper array to sort . In any case, your Sort instance seems temporary (you want to skip it), so why not use a temporary ArrayCollection?

This is what 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--;
}

This is not the most accurate code, it has some strange hacks such as auxLength, not aux.length (this question gave me a range 1 exception), but at least it solved my problem.

+1
source

Source: https://habr.com/ru/post/1714263/


All Articles