Sorting data grid columns numerically in Flash CS3

by Peter deHaan on April 1, 2008

in DataGrid

By default, columns in an ActionScript 3.0 DataGrid control are sorted alphabetically. If the user tries to sort a column containing numeric data the columns may not be sorted as expected. In order to specify that a column contains numeric data you must set the column’s sortOptions property and specify that the column is numeric using the Array.NUMERIC constant.

The following example uses the DataGridColumn class’s sortOptions property to specify that the values in columnB should be sorted numerically when the column’s header is clicked.

Full code after the jump.

/**
 * Requires:
 *   - A DataGrid control in the document's library.
 */
import fl.controls.DataGrid;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.data.DataProvider;
 
var dp:DataProvider = new DataProvider();
dp.addItem({columnA:"Row 1A", columnB:"1234.000"});
dp.addItem({columnA:"Row 2A", columnB:"56.300"});
dp.addItem({columnA:"Row 3A", columnB:"789.123"});
 
var colA:DataGridColumn = new DataGridColumn("columnA");
var colB:DataGridColumn = new DataGridColumn("columnB");
colB.sortOptions = Array.NUMERIC;
 
var myDataGrid:DataGrid = new DataGrid();
myDataGrid.addColumn(colA);
myDataGrid.addColumn(colB);
myDataGrid.dataProvider = dp;
myDataGrid.width = 200;
myDataGrid.rowCount = myDataGrid.length;
myDataGrid.move(10, 10);
addChild(myDataGrid);

{ 3 comments… read them below or add one }

1 exoot 12.30.08 at 10:57 am

how to done this WITHOUT clicking the header!

2 kupendra 04.29.09 at 9:42 pm

hi,
if you have dataprovider as above you can try folling code
dp.sortOn(“columnName”);
myDataGrid.dataProvider = dp;

3 AJ 12.22.09 at 11:13 am

Just wanted to say thanks for the comment kupendra. I searched on the net for a long time trying to figure out how to set the sort column with code. I knew it had to be easy, but there was no information about it. Thanks again.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Previous post:

Next post: