sort字母排序,数字排序,首字母排序

案例1:javascript

在本例中,咱们将建立一个数组,并按字母顺序进行排序:java

<script type="text/javascript">

var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"

document.write(arr + "<br />")
document.write()

</script>arr.sort()

输出:数组

George,John,Thomas,James,Adrew,Martin
Adrew,George,James,John,Martin,Thomas
案例2:

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

</script>arr.sort()

输出:code

10,5,40,25,1000,1
1,10,1000,25,40,5
案例3:

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

</script>arr.sort(sortNumber)

输出:排序

10,5,40,25,1000,1
1,5,10,25,40,1000
相关文章
相关标签/搜索