<script>
var arr = [2, 10, 5, 6];
alert(arr.sort(
function (a, b){
return a-b; // 2, 5, 6, 10
}
)); alert(arr.sort(
function (a, b){
return b-a; // 10, 6, 5, 2
}
));
</script>

解决方案 »

  1.   

    <script language="javascript">
    var arr = [1,2,3,4];
    arr.sort(function(a,b){
    document.write(a+" "+b+"<br>");
    return 1;
    });
    document.write(arr)
    </script>IE7执行结果为
    4 2
    4 3
    4 1
    2 1
    2 3
    2 1
    1 3
    3,1,2,4 
    FireFox 3执行结果为
    1 3
    1 2
    4 1
    1 2
    2 3
    3,2,1,4
      

  2.   

    function参数里的a和b对应Array里的数据元素,来通过一定的大小比较规则来逐一对比,这样来实现排序
      

  3.   

    sort(Array.sort 方法)
    public sort([compareFunction:Object], [options:Number]) :Array对数组中的元素进行排序。Flash 根据 Unicode 值排序。(ASCII 是 Unicode 的一个子集。)默认情况下,Array.sort() 按下面的列表中的说明进行排序:排序区分大小写(Z 优先于 a)。 
    按升序排序(a 优先于 b)。 
    修改该数组以反映排序顺序;在排序后的数组中不按任何特定顺序连续放置具有相同排序字段的多个元素。 
    数值字段按字符串方式进行排序,因此 100 优先于 99,因为 "1" 的字符串值比 "9" 的低。
      

  4.   

    sort 方法 
    返回一个元素已经进行了排序的 Array 对象。
    arr.sort(fun) 参数
    arr是 Array 对象。 
    fun是用来确定元素顺序的函数名。如果这个参数被省略,那么元素将按照 ASCII 字符顺序进行升序排列。说明
    sort 方法将 Array 对象进行适当的排序;在执行过程中并不会创建新的 Array 对象。如果提供了 fun 参数,那么该函数必须返回下列值之一: 
    (1)负值,如果所传递的第一个参数比第二个参数小。 
    (2)零,如果两个参数相等。 
    (3)正值,如果第一个参数比第二个参数大。 示例
    <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);
    }function RandomSort(x, y) {
      return Math.floor(Math.random() * 2 - 1 );
    }var array = [2,4,3,5,1,6,9,0,8];document.write("<p>正序:" + array.sort(AscSort) + "</p>");
    document.write("<p>倒序:" + array.sort(DescSort) + "</p>");
    document.write("<p>随机排序:" + array.sort(RandomSort) + "</p>");
    document.write("<p>随机排序:" + array.sort(RandomSort) + "</p>");
    document.write("<p>随机排序:" + array.sort(RandomSort) + "</p>");
    </script>
      

  5.   

    这里应该是因为array在IE和FF上有一些区别吧
      

  6.   

    我不理解的关键是 fun 参数 ,参数的传递肯定不是随机的,是有一定规律的,我是想知道这个规律,或者是用了什么算法