Flex中Array和ArrayCollection排序
Array排序
protected function applicationCompleteHandler(event:FlexEvent):
void
{
var array:Array = [];
array.push( new Vga("a",10));
array.push( new Vga("c",2));
array.push( new Vga("f",1.3));
array.push( new Vga("d",1.1));
array.push( new Vga("e",16));
array.push( new Vga("b",0));
trace(array.toString());
// output: [a,10],[c,2],[f,1.3],[d,1.1],[e,16],[b,0]
var defaultSort:Array = array.sort(); // 默认排序
trace(defaultSort.toString());
// output: [a,10],[b,0],[c,2],[d,1.1],[e,16],[f,1.3]
var sortFunArray:Array = array.sort(sortFun); // 使用自定义方法排序
trace(sortFunArray.toString());
// output: [b,0],[d,1.1],[f,1.3],[c,2],[a,10],[e,16]
}
/** 自定义排序方法 */
public function sortFun(a:Vga,b:Vga): int{
if(a.price < b.price){
return -1; // a在前,b在后
} else if(a.price == b.price){
return 0; // ab位置不变
} else{
return 1; // b在前,a在后
}
}
/** 排序VO对象 */
public class Vga
{
public var name:String;
public var price:Number;
public function Vga(name:String,price:Number)
{
this.name = name;
this.price = price;
}
public function toString():String{
return "["+ this.name+","+ this.price+"]";
}
}
{
var array:Array = [];
array.push( new Vga("a",10));
array.push( new Vga("c",2));
array.push( new Vga("f",1.3));
array.push( new Vga("d",1.1));
array.push( new Vga("e",16));
array.push( new Vga("b",0));
trace(array.toString());
// output: [a,10],[c,2],[f,1.3],[d,1.1],[e,16],[b,0]
var defaultSort:Array = array.sort(); // 默认排序
trace(defaultSort.toString());
// output: [a,10],[b,0],[c,2],[d,1.1],[e,16],[f,1.3]
var sortFunArray:Array = array.sort(sortFun); // 使用自定义方法排序
trace(sortFunArray.toString());
// output: [b,0],[d,1.1],[f,1.3],[c,2],[a,10],[e,16]
}
/** 自定义排序方法 */
public function sortFun(a:Vga,b:Vga): int{
if(a.price < b.price){
return -1; // a在前,b在后
} else if(a.price == b.price){
return 0; // ab位置不变
} else{
return 1; // b在前,a在后
}
}
/** 排序VO对象 */
public class Vga
{
public var name:String;
public var price:Number;
public function Vga(name:String,price:Number)
{
this.name = name;
this.price = price;
}
public function toString():String{
return "["+ this.name+","+ this.price+"]";
}
}
ArrayCollection排序
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.collections.SortField;
import mx.collections.Sort;
import mx.collections.ArrayCollection;
private var acSort:ArrayCollection=
new ArrayCollection([{id:0,userName:"zhangSan",age:21},
{id:2,userName:"liSi",age:24},
{id:1,userName:"wangWu",age:31}]);
private function sortAc():ArrayCollection{
var sort:Sort= new Sort();
// 按照ID升序排序
sort.fields=[ new SortField("id")];
// 按照userName降序排序
sort.fields=[ new SortField("userName", true, true)];
// 先按ID升序,再按userName降序
sort.fields[ new SortField("id"), new SortField("userName", true, true)];
acSort.sort=sort;
acSort.refresh(); // 更新
return acSort;
}
/*
其实看看API就一目了然
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) — 指示比较运算符是否按编号而不按字母顺序比较排序项目。
*/
]]>
</mx:Script>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.collections.SortField;
import mx.collections.Sort;
import mx.collections.ArrayCollection;
private var acSort:ArrayCollection=
new ArrayCollection([{id:0,userName:"zhangSan",age:21},
{id:2,userName:"liSi",age:24},
{id:1,userName:"wangWu",age:31}]);
private function sortAc():ArrayCollection{
var sort:Sort= new Sort();
// 按照ID升序排序
sort.fields=[ new SortField("id")];
// 按照userName降序排序
sort.fields=[ new SortField("userName", true, true)];
// 先按ID升序,再按userName降序
sort.fields[ new SortField("id"), new SortField("userName", true, true)];
acSort.sort=sort;
acSort.refresh(); // 更新
return acSort;
}
/*
其实看看API就一目了然
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) — 指示比较运算符是否按编号而不按字母顺序比较排序项目。
*/
]]>
</mx:Script>
</mx:Application>