Flex 4.6 ArrayCollection求最大值,最小值,排序
項目需求:求出ArrayCollection的最大值,最小值,排序
源代碼下載地址:http://download.csdn.net/detail/wuxiaokaixinguo/5290274
代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- 將非可視元素(例如服務、值對象)放在此處 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.collections.Sort;
import mx.collections.SortField;
import mx.controls.Alert;
[Bindable]
public var coll:ArrayCollection = new ArrayCollection([
{getInfoTime:'2013-04-23 10:00',waterHeight:6},
{getInfoTime:'2013-04-23 10:02',waterHeight:2},
{getInfoTime:'2013-04-23 10:04',waterHeight:3},
{getInfoTime:'2013-04-23 10:06',waterHeight:1000},
{getInfoTime:'2013-04-23 10:08',waterHeight:5.0},
{getInfoTime:'2013-04-23 10:10',waterHeight:2000}
]);
// Get a function of the maximum
public function btn_getMaxValue_clickHandler():void{
var sort:Sort = new Sort();
//按照waterHeight升序排序
sort.fields=[new SortField("waterHeight")];
coll.sort = sort;
coll.refresh();
Alert.show(coll[coll.length-1].waterHeight);
}
//Get a function of the minimum
public function btn_getMinValue_clickHandler():void{
var sort:Sort = new Sort();
//按照waterHeight升序排序
sort.fields=[new SortField("waterHeight")];
coll.sort = sort;
coll.refresh();
Alert.show(coll[0].waterHeight);
}
]]>
</fx:Script>
<s:Button x="171" y="156" width="83" height="31" label="獲取最大值" click="btn_getMaxValue_clickHandler();"/>
<s:Button x="321" y="157" width="83" height="31" label="獲取最小值" click="btn_getMinValue_clickHandler();"/>
</s:Application>
備用知識:
SortField的用法:
構造函數細節
SortField () 構造函數
public function SortField(name:String = null, caseInsensitive:Boolean = false, descending:Boolean = false, numeric:Object = null)
構造函數。
參數 name:String (default = null) — 此字段用來進行比較的屬性的名稱。如果該對象爲簡單類型,則傳遞 null。
caseInsensitive:Boolean (default = false) — 在對字符串進行排序時,指示比較運算符是否忽略值的大小寫。
descending:Boolean (default = false) — 指示比較運算符是否按降序排列項目。
numeric:Object (default = null) — 指示比較運算符是否按編號而不按字母順序比較排序項目。