Posted on Saturday 14 January 2006
The v2 Datagrid component features column sorting which can be triggered by clicking on a header. The trouble is that it only sorts columns alphabetically; if we have a column of numbers then 100, 20, 3000 is a valid ascending alphabetical order, not 20, 100, 3000 as you would expect. There is no simple way to do this unfortunately, even though it seems like a no-brainer.
The solution:
- Disable automatic sorting on a column
- Listen to the headerRelease event
- Sort manually on headerRelease
The code:
import mx.utils.Delegate;
//Sort column 1 numerically
dg.getColumnAt(1).sortOnHeaderRelease = false;
dg.addEventListener("headerRelease", Delegate.create(this, sortNumerically));
function sortNumerically(evtObj)
{
var i = evtObj.columnIndex;
var col = scores_dg.getColumnAt(i);
if(i == 1)
{
//Sort this column numerically
var dir = col.headerCell.asc ? Array.DESCENDING : 0;
var dp = dg.dataProvider;
if(dp instanceof mx.remoting.RecordSet)
{
dp.sortItemsBy(col.columnName, null, dir | Array.NUMERIC );
}
else
{
dp.sortItemsBy(col.columnName, dir | Array.NUMERIC );
}
col.headerCell.asc = !col.headerCell.asc;
}
}
The rant:
I'll let Jesse fill in that section.


