sort()数字排序中:arr.sort(sortNumber)调用的过程给讲一下!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 + "")document.write(a
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/23 02:42:48
sort()数字排序中:arr.sort(sortNumber)调用的过程给讲一下!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 + "")document.write(a
sort()数字排序中:arr.sort(sortNumber)调用的过程给讲一下!
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 + "
")
document.write(arr.sort(sortNumber))
sort()数字排序中:arr.sort(sortNumber)调用的过程给讲一下!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 + "")document.write(a
sort的参数,为一个函数,那么这个函数接受2个参数,a, b
这个函数在:
a == b 是为0
a > b 时为 1
a < b时 为-1
那么sort在排序的时候,根据冒泡排序,传递2个数进来
对于
arr[0] = "10"
arr[1] = "5"
那么 a=10 b=5,
显然a > b,那么sort根据这个函数返回的结构,就开始排了,
5, 10
那么第一次结束时: 5 10 40 25 1000 1
接下来:然后5 跟 40 25 1000 1排,
完了只有是: 1 10 40 25 1000 5
然后从10 开始,10 - 40 10 <->25 10 <-> 1000 10 <->5
结束后:1 5 40 25 1000 10
再40 : 1 5 10 25 1000 40
再 25 :
再 1000 : 1 5 10 25 40 1000
到此,最后一位就剩一个了,没有啥比较的了,排序完成