forked from: Flex DataGridで汎用的な数値ソート

by nayu forked from Flex DataGridで汎用的な数値ソート (diff: 1)
♥0 | Line 45 | Modified 2010-04-18 08:24:29 | MIT License
play

ActionScript3 source code

/**
 * Copyright nayu ( http://wonderfl.net/user/nayu )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/xmiV
 */

<?xml version="1.0" encoding="utf-8"?>
<!-- forked from fernet's Flex DataGridで汎用的な数値ソート -->
<!--
	Flex DataGridで汎用的な数値ソート
	フィールド名をスクリプト側で直書きしないようにしている
-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
	<mx:Script><![CDATA[
	[Bindable]
	private var mydata:Array = [
		{name: "A", score: "70", grade: "良"},
		{name: "B", score: "8",  grade: "不可"},
		{name: "C", score: "90", grade: "優" },
		{name: "D", score: "60", grade: "可"},
	];
	
	private function NumSortOn(sortField:String):Function
	{
		return function(a:*, b:*):int
		{
			var sub:int = parseInt(a[sortField]) - parseInt(b[sortField]);
			if (sub != 0)
				return sub > 0 ? 1 : -1;
			return 0;
		}
	}
	
	]]></mx:Script>
	<mx:DataGrid dataProvider="{mydata}">
		<mx:columns>
			<mx:DataGridColumn
				headerText="Name"
				dataField="name"
				/>
			<mx:DataGridColumn
				headerText="Score"
				dataField="score"
				sortCompareFunction="{NumSortOn('score')}"
				/>
			<mx:DataGridColumn
				headerText="Grade"
				dataField="grade"
				sortCompareFunction="{NumSortOn('score')}"
				/>
		</mx:columns>
	</mx:DataGrid>
</mx:Application>