| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 790 人关注过本帖
标题:一个搞不太懂的问题~~
只看楼主 加入收藏
w362422
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-5-6
收藏
 问题点数:0 回复次数:2 
一个搞不太懂的问题~~
<html>
<head></head>
 <title>排序</title>
  <body>
  <script language="JavaScript">
  function numCompare(a,b){
    return  a-b;
   }
    nums = new Array(30,10,200,4);
    sortednums = nums.sort(numCompare);//这个程序排序的原理是什么,有点不太明白
 
    document.write(sortednums);
  
   </script>
  </body>
  </html>
2008-05-08 20:34
kingyar
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2008-4-26
收藏
得分:0 
sort函数时Array对象内置的方法,功能是比较数组中每个元素的大小,按照升序进行排列。
2008-05-08 20:38
渚薰
Rank: 6Rank: 6
等 级:贵宾
威 望:22
帖 子:1132
专家分:0
注 册:2006-8-6
收藏
得分:0 
这种设计方式,在.net中叫委托,在java中叫监听。
sort方法的大致内部实现:
Array.prototype.sort=function(compareFunc) {
    var temp;
    for (var i=0;i<this.length-1;i++) {
       for (var j=i+1;j<this.length;j++) {
            if (compareFunc(this[i],this[j])>0) {
                  temp = a[i];
                  a[i]=a[j];
                  a[j]=temp;
            }
       }
    }
}

官方描述如下:
sort
Sorts the elements of an array.
方法源 Array
实现版本 Navigator 3.0, LiveWire 1.0
Navigator 4.0: modified behavior.

语法
sort(compareFunction)
参数
compareFunction Specifies a function that defines the sort order. If omitted, the array is sorted lexicographically (in dictionary order) according to the string conversion of each element.

If compareFunction(a, b) is less than 0, sort b to a lower index than a.

If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements.

If compareFunction(a, b) is greater than 0, sort b to a higher index than a.

个人ajax技术专题站: " target="_blank">http://www. 我不会闲你烦,只会闲你不够烦!
2008-05-09 10:28
快速回复:一个搞不太懂的问题~~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014529 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved