I’ve been helping out with a flex project at work and have been learning some really cool things about it. Coming from a java background a lot of this is familiar but Flex has some really neat features built in.
One that I found recently is the Sort class in the collections package. Pretty much all UI components (Lists, Datagrids, etc) take some kind of collection as their data provider. The question came up, “How do you sort a datagrid manually?” We all know you can click the labels and the grid will sort itself but how do you do that in code. Here’s how:
// Create a new Sort Object
mySort = new Sort();
// Create a SortField Object
// The paramater is the field in the Array Collection to sort on
sortByLabel = new SortField("labelField");
// add Fields to your Sort Object.
// This is standard array notation so multiple sorts would be [sort1,sort2,sort3]
mySort.fields=[sortByLabel];
// set the sort object as the Array Collections sort
myArrayCollection.sort=mySort;
// refresh the Array Collection
myArrayCollection.refresh();
Now, assuming you have bound your data provider in your datagrid (i.e. dataProvider=”{myArrayCollection}”) the datagrid will sort as soon as the Array Collection is refreshed.
A big thanks to Bruce Phillips’ post for pointing me in the right direction.

Related Articles
1 user responded in this post
Thanks for this.
Leave A Reply