<html> |
<head> |
<meta http-equiv= "Content-Type" content= "text/html; charset=gb2312" > |
<title>数组排序</title> |
</head> |
<body> |
<pre> |
<h3>数组排序</h3> |
<script language= "javascript" > |
var tempArray = []; |
var numberArray = [3,324,5345,6546,134,5654,665]; // 定义一个需要排序的数组 |
function SortFunction(a, b) { // 定义排序的方法 |
return -(a - b); // 返回结果差 |
} |
with (document) { |
writeln( '<h4>数字数组numberArray:[3,324,5345,6546,134,5654,665]</h4>' ); |
writeln( '<h4>使用不带参数的sort方法排序,返回的数组如下:</h4>' ); |
tempArray = numberArray.sort( ); // 不带参数的sort方法排序 |
for ( var i = 0; i< tempArray.length; i++) { // 根据数组的个数遍历数组。 |
writeln( '<h4>' +tempArray[i]+ '</h4>' ); // 打印在数组中的值。 |
} |
writeln( '<h4>使用带参数的sort方法排序,返回的数组如下:</h4>' ); |
tempArray = numberArray.sort(SortFunction); // 带参数的sort方法排序 |
for ( var i=0; i<tempArray.length; i++){ // 根据数组的个数遍历数组。 |
writeln( '<h4>' +tempArray[i]+ '</h4>' ); // 打印在数组中的值。 |
} |
} |
document.write( '<h1>' +tempArray.join( "-" )+ '</h1>' ); |
</script> |
</pre> |
</body> |
</html> |