2009-08-03 77 views
3

对我的dataprovider(Array Collection)应用数字排序后,我无法通过tilelist重新排序项目。我需要从arrayCollection中移除排序吗?如果是这样,是否只是设置collection.sort = null?ArrayCollection删除排序

var sortField:SortField=new SortField(); 
sortField.name="order"; 
sortField.numeric=true; 
var sort:Sort=new Sort(); 
sort.fields=[sortField]; 

回答

4

将排序设置为空应该确实会删除集合的排序。您可能需要执行可选的刷新()。

+0

哼哼......实际上,当我设置`sort = null`并尝试编辑`ArrayCollection`时,我得到一个异常。除非我调用`ArrayCollection.refresh()`,但是当我这样做时,即使'sort`为'null',数据也会被排序。我卡住了:p – nununo 2011-05-28 16:18:12

1

Source

Adob​​e Flex的 - 按日期排序的ArrayCollection

/** 
* @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

我得到了这个问题抓过,我发现你的问题,我仍然没有得到它解决了像克里斯托夫建议。

经过一段时间的苦难,我发现了一种避免你提到的问题的方法。

只需使用辅助ArrayCollection进行排序。无论如何,你的排序实例似乎是临时的(你想通过它),所以为什么不使用临时ArrayCollection?

这里是我的代码是如何模样:

// 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--; 
} 

这不是最巧妙的代码,它有一个像auxLength代替aux.length一些奇怪的黑客(此人给我-1阵列范围除外),但在至少它解决了我的问题。