## javascript的sort函数的参数

sort()函数在w3school的教程入门上已经明确的说明了它可以有一个形参，并且如果有参数那么这个参数一定是一个函数，而且还给了相应的例子：

```定义和用法
sort() 方法用于对数组的元素进行排序。

arrayObject.sort(sortby)

sortby	可选。规定排序顺序。必须是函数。```

```<script type="text/javascript">

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())

</script>

10,5,40,25,1000,1
1,10,1000,25,40,5```

```<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>

10,5,40,25,1000,1
1,5,10,25,40,1000```

```     function InsertionSort(a, from, to) {
for (var i = from + 1; i < to; i++) {
var element = a[i];
for (var j = i - 1; j >= from; j--) {
var tmp = a[j];
var order = comparefn(tmp, element);
if (order > 0) {
a[j + 1] = tmp;
} else {
break;
}
}
a[j + 1] = element;
}
};```