js的數組中有一個sort方法,默認是按照ASCII 字符順序進行升序排列。如果需要自定義排序,例如希望通過二維數組中的第二列數據排序:
<script language="javascript">
<!--
var testArray = new Array();
testArray[0]= new Array('e','2','g');
testArray[1]= new Array('b','3','c');
testArray[2]= new Array('a','1','a');
testArray[3]= new Array('d','4','h');
testArray.sort(sortfunction);
alert(testArray[0] + "" + testArray[1] + "" + testArray[2] + "" + testArray[3])
// 排序方法中有兩個參數,表示數組中兩個用來排序的元素
//
function sortfunction(x,y)
{
return x[2].charCodeAt(0) - y[2].charCodeAt(0);//根據二維數組的第三列的第一個字母的ASCII碼來降序排序
}
// -->
</script>
新聞熱點
疑難解答