Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting 2D array with numbers 1

Status
Not open for further replies.

SarahKate31

Programmer
Feb 14, 2002
34
US
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='javascript:eek:nclick=ArrSort()'>Profit</A></td>
</tr>
</table>
</body>
</html>
 
Array indexes are messed up (for example [0][0] is reused twice), sort comparison function expects numbers while you sent arrays, error handler doesn't help much (use alert(e.description) instead) and used <A href ...> is wrong.

Try this:
Code:
<script language=javascript>
function ArrSort() {
  var tempArr=new Array()
  
  tempArr[0] = [ 700, "John" ];
  tempArr[1] = [  50, "Ryan" ];
  tempArr[2] = [ 100, "Nicole" ];

  try{tempArr.sort(numberSort)} catch(e){alert("Error in sort(): " & e.description)}
  
	for(sDebug="", i=0;i<tempArr.length;i++)	sDebug+= tempArr[i].join(", ") + "\n";
	alert( sDebug );
}

function numberSort(a,b) {
  return(a[0]-b[0])
}
</script>
...
<A href="#" onclick="javascript:ArrSort()">Profit</A>

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
I simplified the code for posting and the array index mistakes were typos...but thank you for the revision to numberSort:

return(a[0]-b[0])

that did the trick...Also, I haven't had any trouble with the link...how come its wrong?

thanks -- kate
 
Because you're assigning the output of the ArrSort() function to a window variable "onclick". Basically you have an extra step in there that does nothing.

Adam

for(ring=0;ring<rosie;ring++){pocket[ring]='posie';ashes*=2;fall--}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top