下面是代碼的關鍵
if(obj.data!=null){
if(userDb!=null){
var sortFields:Sort=userDb.sort;
userDb=obj.data as ArrayCollection;
userDb.sort=sortFields;
userDb.refresh();
}else{
userDb=obj.data as ArrayCollection;
}
}
備註:userDb.refresh();加上這句話排序才起作用。
下面的代碼是關於ArrayCollection的排序的
假設ArrayCollection(m_myArrayCollection)屬性有userID,userName,regTime。
1、按regTime排序
程序代碼
var m_myArrayCollection:ArrayCollection = new ArrayCollection();
//先加入N個測試object
m_myArrayCollection.addItem({userID:0,userName:AAA,regTime:2008-02-28},{userID:1,userName:BBB,regTime:2008-02-29},{userID:2,userName:CCC,regTime:2008-03-01});
//設定Sort對象
var m_sort:Sort = new Sort();
//默認按升序排序
m_Sort.fields = [new SortField("regTime")];
//按降序排序,把上一句註釋,比對一下效果
//m_Sort.fields = [new SortField("regTime",true,true)];
//把排序方法指定給m_myArrayCollection
m_myArrayCollection.sort = m_sort;
//如果不執行refresh,啥事都不發生
m_myArrayCollection.refresh();
2、先按userID降序排序,再按userName升序排序
程序代碼
var m_myArrayCollection:ArrayCollection = new ArrayCollection();
//先加入N個測試object
m_myArrayCollection.addItem({userID:0,userName:AAA,regTime:2008-02-28},{userID:1,userName:BBB,regTime:2008-02-29},{userID:2,userName:CCC,regTime:2008-03-01});
//設定Sort對象
var m_sort:Sort = new Sort();
//默認按升序排序
m_Sort.fields = [new SortField("userID",true,true),new SortField("userName")];
//把排序方法指定給m_myArrayCollection
m_myArrayCollection.sort = m_sort;
//如果不執行refresh,啥事都不發生
m_myArrayCollection.refresh();