5.10. 数组排序
问题
我要进行数组排序
解决办法
使用 sort( ) 方法,对于对象数组可以用 sortOn( ) 方法
讨论
使用 sort( ) 方法就可以对数组进行排序,没有参数是进行升序排序,对于字符内容采用 Unicode 编码排序
var words:Array = ["tricycle", "relative", "aardvark", "jargon"];
words.sort( );
trace(words); // 显示: aardvark,jargon,relative,tricycle
如果要进行降序排序,需要传递参数Array.DESCENDING 常量:
var words:Array = ["tricycle", "relative", "aardvark", "jargon"];
words.sort(Array.DESCENDING);
trace(words); // 显示: tricycle,relative,jargon,aardvark
上面的例子没有考虑大小写问题,比如:
var words:Array = ["Tricycle", "relative", "aardvark", "jargon"];
words.sort( );
trace(words); // 显示: Tricycle,aardvark,jargon,relative
使用Array.CASEINSENSITIVE 常量忽略大小写进行排序:
var words:Array = ["Tricycle", "relative", "aardvark", "jargon"];
words.sort(Array.CASEINSENSITIVE);
trace(words); // 显示 aardvark,jargon,relative,Tricycle
如果对数字内容的数组排序,则根据第一个数字的ASCII 排序:
var scores:Array = [10, 2, 14, 5, 8, 20, 19, 6];
scores.sort( );
trace(scores); // 显示: 10,14,19,2,20,5,6,8
使用 Array.NUMERIC 常量才能正常对数字排序:
var scores:Array = [10, 2, 14, 5, 8, 20, 19, 6];
scores.sort(Array.NUMERIC);
trace(scores); // 显示:2,5,6,8,10,14,19,20
还有两个常量Array.UNIQUESORT 和 array.RETURNINDEXEDARRAY. ,如果你只是对含有唯一元素的数组排序就可以用Array.UNIQUESORT ,Flash只会对这样的数组排序,不满足条件 sort( ) 返回0,且不进行排序:
var ranking:Array = [2,5,6,3,1,1,4,8,7,10,9];
var sortedRanking:Object = ranking.sort(Array.UNIQUESORT);
trace(sortedRanking); // 显示: 0
trace(ranking); // 显示:2,5,6,3,1,1,4,8,7,10,9
Array.RETURNINDEXEDARRAY 得到排序后数组元素的下标顺序,但不改变原始数组:
var words:Array = ["tricycle", "relative", "aardvark", "jargon"];
var indices:Array = words.sort(Array.RETURNINDEXEDARRAY);
trace(words); // 显示: tricycle,relative,aardvark,jargon
trace(indices); // 显示: 2,3,1,0
for(var i:int = 0; i < words.length; i++) {
/* 显示
aardvark
jargon
relative
tricycle
*/
trace(words[indices[i]]);
}
可以用操作符(|)联合使用这些常量:
var words:Array = ["Tricycle", "relative", "aardvark", "jargon"];
words.sort(Array.CASEINSENSITIVE | Array.DESCENDING);
trace(words); // 显示: Tricycle,relative,jargon,aardvark
有时候你想反转数组该怎么办呢?sort( ) 方法并没这功能,这是可以用 reverse( ) 方法:
var words:Array = ["tricycle", "relative", "aardvark", "jargon"];
words.reverse( );
trace(words); // 显示: jargon,aardvark,relative,tricycle
上面的部分讨论了怎么对字符串和数字进行排序,对于对象数组可用 sortOn( ) 方法,该方法需要一个字符串参数指定名称属性对其排序:
var cars:Array = new Array();
cars.push({make: "Honda", year: 1997, color: "maroon"});
cars.push({make: "Chrysler", year: 2000, color: "beige"});
cars.push({make: "Mercedes", year: 1985, color: "blue"});
cars.push({make: "Fiat", year: 1983, color: "gray"});
// 对 year 属性进行排序 cars.sortOn("year"):
for (var i:int = 0; i < cars.length; i++) {
/* 显示:
gray 1983 Fiat
blue 1985 Mercedes
maroon 1997 Honda
beige 2000 Chrysler
*/
trace(cars[i].color + "/t" +
cars[i].year + "/t" +
cars[i].make);
}
sortOn( ) 方法也可以一次对多个字段进行排序,看下面的代码:
var cars:Array = new Array( );
cars.push({make: "Honda", year: 1997, color: "maroon"});
cars.push({make: "Chrysler", year: 2000, color: "beige"});
cars.push({make: "Mercedes", year: 1985, color: "blue"});
cars.push({make: "Fiat", year: 1983, color: "gray"});
cars.push({make: "Honda", year: 1992, color: "silver"});
cars.push({make: "Chrysler", year: 1968, color: "gold"});
cars.push({make: "Mercedes", year: 1975, color: "green"});
cars.push({make: "Fiat", year: 1983, color: "black"});
cars.push({make: "Honda", year: 2001, color: "blue"});
cars.push({make: "Chrysler", year: 2004, color: "orange"});
cars.push({make: "Mercedes", year: 2000, color: "white"});
cars.push({make: "Fiat", year: 1975, color: "yellow"});
// 对两个字段排序
cars.sortOn(["year", "make"]);
for (var i:int = 0; i < cars.length; i++) {
/* 显示:
gold 1968 Chrysler
yellow 1975 Fiat
green 1975 Mercedes
black 1983 Fiat
gray 1983 Fiat
blue 1985 Mercedes
silver 1992 Honda
maroon 1997 Honda
beige 2000 Chrysler
white 2000 Mercedes
blue 2001 Honda
orange 2004 Chrysler
*/
trace(cars[i].color + "/t" +
cars[i].year + "/t" +
cars[i].make);
}
下面的例子,先对 make, 再对year排序:
cars.sortOn(["make", "year"]);
for (var i:int = 0; i < cars.length; i++) {
/* 显示:
gold 1968 Chrysler
beige 2000 Chrysler
orange 2004 Chrysler
yellow 1975 Fiat
black 1983 Fiat
gray 1983 Fiat
silver 1992 Honda
maroon 1997 Honda
blue 2001 Honda
green 1975 Mercedes
blue 1985 Mercedes
white 2000 Mercedes
*/
trace(cars[i].color + "/t" +
cars[i].year + "/t" +
cars[i].make);
}
sortOn( ) 方法也可用那些数组常量完成降序,忽略大小写等排序:
cars.sortOn("year", Array.DESCENDING);
for (var i:int = 0; i < cars.length; i++) {
/* 显示:
beige 2000 Chrysler
maroon 1997 Honda
blue 1985 Mercedes
gray 1983 Fiat
*/
trace(cars[i].color + "/t" +
cars[i].year + "/t" +
cars[i].make);
}