bherrington
03-13-2004, 08:57 AM
I am trying to sort, and then reference, an array of information in a league table, used for a fantasy motor racing league. I need to record (amongst other things) each team's name, it's points scored in a number of races, and a total score for the season, such as;
Linda, 7 ,4 ,...etc....,11)
David, 5 ,4 ,...etc....,9)
Henry, 9 ,3 ,...etc....,12)
All of these results will be stored as variables from other JS calculations, but for simplicity I've listed them here as values. What I want to do is display the results in a table in the right order ranked by total score. I have seen sorting example scripts on the web, but all of these involve sorting a table - however, in reale life I am using a table of 117 rows and 21 columns, with colour formatting to display the results, so the process of drawing table/sorting table/redrawing table is awfully long, I want to sort the data first, then just draw the table once.
I started just by trying to sort results alphabetically by team name (only using owner name, 2 race results and total for simplicity);
<script Language="Javascript1.2">
var s = new Array()
s[0] = new Array('Linda',7,4,11);
s[1] = new Array('David',5,4,9);
s[2] = new Array('Henry',9,3,12);
function calculate(){
ownernamesortHigh = s.sort();
ownernamesortLow = ownernamesortHigh.reverse()
}
</script>
and then reference the rresulting sorted/reversed arrays using
<script Language="Javascript1.2">
calculate()
document.write("1st place with total score = "+ ownernamesortHigh[0][3] + ", Owner = " + ownernamesortHigh[0][0])
document.write("<br>")
document.write("3rd place with total score = "+ ownernamesortLow[0][3] + ", Owner = " + ownernamesortLow[0][0])
</script>
just to display owner name and their total score for testing purposes (ultimately I would write it into a table), but I can't even get the sort and reverse methods to work correctly, never mind try to sort the array numerically by total score by identifying a value other than the first values in the 2nd level arrays of 'array s' to sort.
Any suggestions on what I'm doing wrong?
Linda, 7 ,4 ,...etc....,11)
David, 5 ,4 ,...etc....,9)
Henry, 9 ,3 ,...etc....,12)
All of these results will be stored as variables from other JS calculations, but for simplicity I've listed them here as values. What I want to do is display the results in a table in the right order ranked by total score. I have seen sorting example scripts on the web, but all of these involve sorting a table - however, in reale life I am using a table of 117 rows and 21 columns, with colour formatting to display the results, so the process of drawing table/sorting table/redrawing table is awfully long, I want to sort the data first, then just draw the table once.
I started just by trying to sort results alphabetically by team name (only using owner name, 2 race results and total for simplicity);
<script Language="Javascript1.2">
var s = new Array()
s[0] = new Array('Linda',7,4,11);
s[1] = new Array('David',5,4,9);
s[2] = new Array('Henry',9,3,12);
function calculate(){
ownernamesortHigh = s.sort();
ownernamesortLow = ownernamesortHigh.reverse()
}
</script>
and then reference the rresulting sorted/reversed arrays using
<script Language="Javascript1.2">
calculate()
document.write("1st place with total score = "+ ownernamesortHigh[0][3] + ", Owner = " + ownernamesortHigh[0][0])
document.write("<br>")
document.write("3rd place with total score = "+ ownernamesortLow[0][3] + ", Owner = " + ownernamesortLow[0][0])
</script>
just to display owner name and their total score for testing purposes (ultimately I would write it into a table), but I can't even get the sort and reverse methods to work correctly, never mind try to sort the array numerically by total score by identifying a value other than the first values in the 2nd level arrays of 'array s' to sort.
Any suggestions on what I'm doing wrong?