分享

Reusable Numeric Sort Method for Flex DataGrid Columns

 易志娃的图书馆 2013-10-12

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");

Post to Twitter Tweet This Post

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约