以前我在博客里介绍过的一个文章:
http://blog.csdn.net/wide288/article/details/8573611
本文章的排序测试过,IE8, CHROME 24, firefox 17.
arrayobj.sort(sortfunction)参数
arrayObj
数组
sortFunction
可选项。比较函数。如果这个参数被省略,那么元素将按照 ASCII 字符顺序进行升序排列。
比较函数必须返回下列值之一:
* 负值,如果所传递的第一个参数比第二个参数小。
* 零,如果两个参数相等。
* 正值,如果第一个参数比第二个参数大
----------------------------------------------------
test1.html
< html >
< body >
< script type ="text/javascript" >
var y = new Array( 10 , 30000 , 12 );
y.sort();
alert(y);
</ script >
</ body >
</ html >
排序结果正确(按我自认为从小到大的顺序,暂且把这种观点认为是正确的)。
-----------------------------------------------------------------
test2.html
< html >
< body >
< script type ="text/javascript" >
var y = new Array( 36000 , 500 , 10100 );
y.sort();
alert(y);
</ script >
</ body >
</ html >
排序结果不正确。
----------------------------------------------------------------
test2.html
< html >
< body >
< script type ="text/javascript" >
function AscSort(x, y) {
return x == y ? 0 : ( x > y ? 1 : -1 );
}
function DescSort(x, y) {
return x == y ? 0 : ( x > y ? -1 : 1 );
}
var y = new Array( 36000 , 500 , 10100 );
y.sort(AscSort);
alert(y);
</ script >
</ body >
</ html >
--------------------------------------------------
得到我所谓的正确排序了