function sort1(a,b) { return a - b;}
function sort2(a,b) { return a + b;}
var arr = new Array(2,1,4,3);
document.write(arr+"<br>");
document.write(arr.sort(sort1)+"<br>");
document.write(arr.sort(sort2)+"<br>");
输出是:
2,1,4,3
1,2,3,4
3,1,2,4
我实在不明白了,麻烦大虾给我讲一下那个函数参数到底是怎么使用的呢?
为什么返回a-b就升序,而返回a+b就那样子了啊?
function sort2(a,b) { return a + b;}
var arr = new Array(2,1,4,3);
document.write(arr+"<br>");
document.write(arr.sort(sort1)+"<br>");
document.write(arr.sort(sort2)+"<br>");
输出是:
2,1,4,3
1,2,3,4
3,1,2,4
我实在不明白了,麻烦大虾给我讲一下那个函数参数到底是怎么使用的呢?
为什么返回a-b就升序,而返回a+b就那样子了啊?
解决方案 »
- Extjs 显示不了 求高手帮忙啊!!
- js文件第一次在IE中加载不了
- 急!文本框,限制首字母大写。
- var arrayA=xmldom.selectNodes("//sheng/itemid[@value=\"1\"]");
- 为什么这个js没有效果啊
- 请教javascript里边可以嵌入其他的js么?
- 关于页面打印不全的问题!
- 如何让我的HTML页面在800*600和1024*768显示时一样都是完全显示,而没有滑动条?
- 我设置了一个Cookie,为什么用window.open打开后读不出Cookie的值?
- 我想问一个问题:在不用站点浏览的情况下,只是双击打开一个文件夹里的一个htm文件,这个文件里的按钮又点开了另外的htm文件,如果第一个htm文件的javascript 里有一个变量,能传到第二个htm文件的javascript 里吗?90分送上
- lightbox是展示图片的 可是不能加表格
- 怎样获取鼠标选取的链接?
<script type="text/javascript">function sortNumber(a,b)
{
return a - b
}var arr = new Array(6)
arr[0] = "10"
arr[1] = "5"
arr[2] = "40"
arr[3] = "25"
arr[4] = "1000"
arr[5] = "1"document.write(arr + "<br />")
document.write(arr.sort(sortNumber))</script>楼主建议一下你运行看看打印出来的
<script type="text/javascript">
function sort1(a,b) { return a - b;}
function sort2(a,b) { return a + b;}
var arr = new Array(2,1,4,3,5);
document.write(arr+"<br>");
document.write(arr.sort(sort1)+"<br>");
document.write(arr.sort(sort2)+"<br>");
</script>
结果:
2,1,4,3,5
1,2,3,4,5
3,5,1,2,4
2---------------------------------------
<script type="text/javascript">
function sort1(a,b) { return a - b;}
function sort2(a,b) { return a + b;}
var arr = new Array(2,1,4,3,5,6);
document.write(arr+"<br>");
document.write(arr.sort(sort1)+"<br>");
document.write(arr.sort(sort2)+"<br>");
</script>
结果:
2,1,4,3,5,6
1,2,3,4,5,6
6,5,3,1,2,4
=3===================================
<script type="text/javascript">
function sort1(a,b) { return a - b;}
function sort2(a,b) { return a + b;}
var arr = new Array(2,1,4,3,5,6,7);
document.write(arr+"<br>");
document.write(arr.sort(sort1)+"<br>");
document.write(arr.sort(sort2)+"<br>");
</script>
结果:
2,1,4,3,5,6,7
1,2,3,4,5,6,7
6,5,3,7,1,2,4
===4================================
<script type="text/javascript">
function sort1(a,b) { return a - b;}
function sort2(a,b) { return a + b;}
var arr = new Array(2,1,4,3,5,6,7,8);
document.write(arr+"<br>");
document.write(arr.sort(sort1)+"<br>");
document.write(arr.sort(sort2)+"<br>");
</script>
结果:
2,1,4,3,5,6,7,8
1,2,3,4,5,6,7,8
6,5,3,7,1,2,4,8
====last====================
<script type="text/javascript">
function sort1(a,b) { return a - b;}
function sort2(a,b) { return a + b;}
var arr = new Array(2,1,4,3,5,6,7,8,9);
document.write(arr+"<br>");
document.write(arr.sort(sort1)+"<br>");
document.write(arr.sort(sort2)+"<br>");
</script>
结果:
2,1,4,3,5,6,7,8,9
1,2,3,4,5,6,7,8,9
6,5,3,7,9,1,2,4,8