SarahKate31
Programmer
Hi everyone --
I have a 2D array with numerical values in the 1st index and string values in the 2nd index. I need to sort the array by the 1st index. I know I can sort a 2D array by the first index when all of the values are strings. And I know I can sort a 1D array of numerical values with the custom numberSort function. When I try to sort this array, I get an error. I need to keep the names associated with their values after sorting is complete. Does anyone know why this is causing an error? Any ideas will be much appreciated. Let me know if I have not given enough info.
Thanks -- Kate
<html>
<head>
<Script language=javascript>
function ArrSort() {
var tempArr=new Array()
tempArr[0]=new Array()
tempArr[0][0]=700
tempArr[0][0]="John"
tempArr[1]=new Array()
tempArr[1][0]=50
tempArr[1][0]="Ryan"
tempArr[2]=new Array()
tempArr[2][0]=100
tempArr[2][0]="Nicole"
try{tempArr.sort(numberSort)} catch(e){alert("error")}
return tempArr;
}
function numberSort(a,b) {
return(a-b)
}
</script>
</head>
<body>
<table>
<tr>
<td><A href='javascriptnclick=ArrSort()'>Profit</A></td>
</tr>
</table>
</body>
</html>
I have a 2D array with numerical values in the 1st index and string values in the 2nd index. I need to sort the array by the 1st index. I know I can sort a 2D array by the first index when all of the values are strings. And I know I can sort a 1D array of numerical values with the custom numberSort function. When I try to sort this array, I get an error. I need to keep the names associated with their values after sorting is complete. Does anyone know why this is causing an error? Any ideas will be much appreciated. Let me know if I have not given enough info.
Thanks -- Kate
<html>
<head>
<Script language=javascript>
function ArrSort() {
var tempArr=new Array()
tempArr[0]=new Array()
tempArr[0][0]=700
tempArr[0][0]="John"
tempArr[1]=new Array()
tempArr[1][0]=50
tempArr[1][0]="Ryan"
tempArr[2]=new Array()
tempArr[2][0]=100
tempArr[2][0]="Nicole"
try{tempArr.sort(numberSort)} catch(e){alert("error")}
return tempArr;
}
function numberSort(a,b) {
return(a-b)
}
</script>
</head>
<body>
<table>
<tr>
<td><A href='javascriptnclick=ArrSort()'>Profit</A></td>
</tr>
</table>
</body>
</html>