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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help sorting an array 2

Status
Not open for further replies.

LyndonOHRC

Programmer
Sep 8, 2005
603
US
This should be simple but I can't seem to spot what I'm doing wrong.

I have some form elements I'm trying to sort and display in a <div>. With this method the output returns "undefined" on every iteration.

Code:
var AllValues = new Array(HowManyScans);
for (i=1; i<=HowManyScans; i++){
	var elemName='CurrentValue'+i;
	var getelem=document.getElementById(elemName);
	AllValues[i]=getelem.value;
}
DisplayDiv.innerHTML='';
[COLOR=red]AllValuesSort = AllValues.sort()[/color];
for (i=1; i<=HowManyScans; i++){
	DisplayDiv.innerHTML=DisplayDiv.innerHTML+'<br />'+i+'. '+[COLOR=red]AllValuesSort[i][/color];
}
document.Scan.ScannedLabel.value='';
document.Scan.ScannedLabel.focus();

If I skip the sort it displays as I expected; except, of course, the elements are not sorted.

Code:
var AllValues = new Array(HowManyScans);
for (i=1; i<=HowManyScans; i++){
	var elemName='CurrentValue'+i;
	var getelem=document.getElementById(elemName);
	AllValues[i]=getelem.value;
}
DisplayDiv.innerHTML='';
[COLOR=red]//AllValuesSort = AllValues.sort()[/color];
for (i=1; i<=HowManyScans; i++){
	DisplayDiv.innerHTML=DisplayDiv.innerHTML+'<br />'+i+'. '+[COLOR=red]AllValues[i][/color];
}
document.Scan.ScannedLabel.value='';
document.Scan.ScannedLabel.focus();

Thanks

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
Hmm - that didn't come out quite right!... Let me try again ;-)

On the second loop (that outputs the data), you probably want to loop from 0 to i<HowManyScans, as afetr sorting, the data in the array will start at index 0...

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Lyndon, using your code I made these changes and it all worked fine:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>title test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

window.onload = function () {

   var DisplayDiv = document.getElementById("DisplayDiv");
   var HowManyScans = 9;
   var AllValues = new Array(HowManyScans);
   for (i=1; i<=HowManyScans; i++){
       var elemName='CurrentValue'+i;
       var getelem=document.getElementById(elemName);
       AllValues[i]=getelem.value;
   }
   DisplayDiv.innerHTML='';
   AllValuesSort = AllValues.sort();
   [gray][i]//if you want to sort numerically instead of strings, use this line instead
   //AllValuesSort = AllValues.sort(sortNumber);[/i][/gray]
   for (i=1; i<=HowManyScans; i++){
       DisplayDiv.innerHTML=DisplayDiv.innerHTML+'<br />'+[!](i - 1)[/!]+'. '+AllValuesSort[[!](i - 1)[/!]];
   }

   function sortNumber(a, b) {
      return a - b;
   }
   
}

</script>
<style type="text/css"></style>
</head>
<body>

<input type="text" id="CurrentValue1" value="234" />
<input type="text" id="CurrentValue2" value="20" />
<input type="text" id="CurrentValue3" value="1617" />
<input type="text" id="CurrentValue4" value="233" />
<input type="text" id="CurrentValue5" value="245" />
<input type="text" id="CurrentValue6" value="982736" />
<input type="text" id="CurrentValue7" value="1" />
<input type="text" id="CurrentValue8" value="8970" />
<input type="text" id="CurrentValue9" value="31613" />
<br /><br /><br />
<div id="DisplayDiv"></div>
</body>
</html>

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
I need to tattoo "array elements are zero based" on the back of my hand! It's got me before.

Thanks to you both.



Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top