Pay for Hesitation: Sorting via ArrayCollection

Pages

2008年3月25日 星期二

Sorting via ArrayCollection

假設ArrayCollection裡面的elements的屬性有userid, username, regtime, 我們想要依照特定欄位進行排序(像datagrid點欄位名稱一樣), 如先對A欄位再對B欄位做排序...

var myArrayCollection:ArrayCollection = new ArrayCollection();
myArrayCollection.addItem({userid:0, username:John, regtime:080323},{userid:1, username:Mary, regtime:080324},{userid:2, username:Tom, regtime:080325});

var mySort:Sort = new Sort();

//下面三行取一中任一行為一種排序方式..
//主要是透過SortField提供排序的資訊, 第一個參數是欄位名稱, 第二個是是否分大小寫, 第三是遞增或遞減
//1. 預設是遞增排序
mySort.fields = [new SortField("regtime")];
//2. 按照遞減排序
mySort.fields = [new SortField("regtime", true, true)];
//3. 先對userid遞減排序, 再對username遞增排序
mySort.fields = [new SortField("userid", true, true), new SortField("username")]

//將排序的方式assign給ArrayCollection
myArrayCollection.sort = mySort;
//千萬記得要呼叫refresh()才會幫我們排序喔!
myArrayCollection.refresh();

沒有留言: