Click to See Complete Forum and Search --> : Sorting arrays and referencing array items after sorting


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?

Khalid Ali
03-13-2004, 10:46 AM
sort does work,show us the page whre you have implemented this code.
So that some one can take a look.

Pittimann
03-13-2004, 11:24 AM
Hi!

I think, the easiest way to get what you want is, to put the total as the first array element (as a string with leading zeros!). If you like, check this out:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
var s = new Array()
s[0] = new Array("11",'Linda',7,4);
s[1] = new Array("09",'David',5,4);
s[2] = new Array("12",'Henry',9,3);
s.sort();
s.reverse();
document.write("1st place with total score = "+ Number(s[0][0]) + ", Owner = " + s[0][1])
document.write("<br>")
document.write("2nd place with total score = "+ Number(s[1][0]) + ", Owner = " + s[1][1])
document.write("<br>")
document.write("3rd place with total score = "+ Number(s[2][0]) + ", Owner = " + s[2][1])
//-->
</script>
</head>
<body>
</body>
</html>

Cheers - Pit

bherrington
03-13-2004, 11:29 AM
:) Thanks for any assistance you can give. I've loaded the page at http://www.tggpc.co.uk/TestSort1.htm but the complete page code is laid out here. If you want to see the full scale of the data I'm trying to sort and display in real life, you can take a look at the monster table at http://www.tggpc.co.uk/Results3.htm
____________________________________
<html>

<head>
<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>
</head>

<body>
<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>

</body>

</html>
___________________________________________


This displays 2 lines of output, which should (in my obviously misguided belief) display on line 1 the name of the team with the first name alphabetically ('David'), with that team's corresponding total score (9), and on line 2 the last team alphabetically ('Linda') with it's total score (11). The problems I have are;

1. Both lines display 'Linda' with total score 11, so it appears that array s is not being sorted, nor the sorted result reversed. :confused:

2. I actually only wanted to sort alphabetically by the owner names (the first entry in the arrays) as a first step towards working out how to sort numerically by the total score (the last item in each array), plus maybe if it was relatively easy to do, giving the user the option to sort by individual race scores as well. :confused: :confused:

BRUCE

Pittimann
03-13-2004, 11:35 AM
Hi!

It seems you typed your last message while I posted and you didn't see my post (scroll up, please, if you are at the bottom of the page)...

I'll have a look at your code, too.

Cheers - Pit

bherrington
03-13-2004, 12:43 PM
It seems you typed your last message while I posted and you didn't see my post (scroll up, please, if you are at the bottom of the page)...

Yes, thanks for this. I can see your code works, and it's nice and simple. However it does have a disadvantage, which is that you've created the total as a string (which makes the sort method work OK). However I will need this value to be in the form of a number, therefore it's not going to work in this format. How can I change the sort approach to change it to a numerical sort?

The other main difference from yours to mine is that yours makes reference to the original array s when using document.write to display values. I thought that the original array s would be unchanged, and that I therefore needed to assign the results of sorting s to a new array, or at least to a variable. It appears then that following the sort, array s is changed to that new configuration for the duration of the user being on this page? I can't see this being a problem for me, but is it possible to assign the result to a new array/variable and make reference to the results in the same way (which is what I was trying to do originally)?

Also, what additions would I need to make to this code to sort on a specific index of the array, eg if I wanted to add functionality to sort by name or other scores?

Pittimann
03-13-2004, 12:52 PM
Hi!

As far as I know you cannot (in js):

1. do a numeric sort, if not all parts of the array(s) are numeric

2. sort an array on a specific index

That's why I "fooled" the sort function by putting the total at first and make it a string (leading zeros for proper sorting). This shouldn't be a problem, if you need it as a number (you can see that in my example: the numeric value of total is displayed - no string with a leading zero; example "09"=>9).

Good luck!

Cheers - Pit