I can’t take full credit for this since I found some version of this function out there somewhere, but since it’s not that easy to find I thought I’d repost it. If you need to numerically sort multiple columns in a DataGrid without creating a separate method for each based on its dataField, then use this: /** * Numerically sorts a column in the DataGrid. * * @param fieldName The string name for the dataField in the column that you want to sort. */ private function numericSortByField(fieldName:String):Function { return function(obj1:Object, obj2:Object):int { var value1:Number = (obj1[fieldName] == '' || obj1[fieldName] == null) ? null : new Number(obj1[fieldName]); var value2:Number = (obj2[fieldName] == '' || obj2[fieldName] == null) ? null : new Number(obj2[fieldName]); return ObjectUtil.numericCompare(value1, value2); } } By default, the DataGrid’s sort converts your data to strings so this ensures it’s really doing a numeric sort (and is a generic one at that). Finish it off by making sure to set the sortCompareFunction for your columns like so: myColumn.sortCompareFunction = this.numericSortByField("myDataField"); |
|