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

Sorting problem

Status
Not open for further replies.

Peter74

Technical User
Jul 27, 2003
10
DK
Hello,

I want to make a table of authors sorted alphabetically, but the script on my page (se below) is wrong as it sorts the array "author" like this:
author 6
author 8
author 2
author 4
author 1
author 5
author 3
author 7

the html-page looks like this:
Sorry for all the code :)

BEGIN PAGE
<html>
<head>
<script>
var author = new Array()
author[0] = &quot;author 5&quot;
author[1] = &quot;author 3&quot;
author[2] = &quot;author 7&quot;
author[3] = &quot;author 4&quot;
author[4] = &quot;author 6&quot;
author[5] = &quot;author 1&quot;
author[6] = &quot;author 8&quot;
author[7] = &quot;author 2&quot;

var sortArray = new Array(); //global array

function sort(sortOrder) {

var tmp = &quot;&quot;;
//tmpArray is used so that I don't make any changes to the
//original array.
var tmpArray = new Array();
tmpArray = author;

//fill sortArray with numbers to be swaped around
//the same way as tmpArrays elements. This will
//be used as the sorting-order when building the table.
for(var i=0; i<author.length; i++) {
sortArray = i;
}

do {
b = &quot;false&quot;;

for(var i = 0; i < tmpArray.length-1; i++) {
if (tmpArray > tmpArray[i+1]) {
// swap them
var tmp = tmpArray;
tmpArray = tmpArray[i+1];
tmpArray[i+1] = tmp;

//sortArray contains the order in which to
//retrieve elements from the
//author-array
var temp = sortArray;
sortArray = sortArray[i+1];
sortArray[i+1] = temp;

b = &quot;true&quot;; //continue iteration
}
}
} while(b == &quot;true&quot;);
}

function buildTable() {

sort()
var table = &quot;<table>&quot;;

for(var i = 0; i < sortArray.length; i++) {
table += &quot;<tr><td>&quot; + author[sortArray] + &quot;</td></tr>&quot;;
}//for

table += &quot;</table>&quot;;

document.write(table);
}
</script>
</head>
<body>
<script>
buildTable();
</script>
</body>
</html>
END PAGE

I have to make code like this:
table += &quot;<tr><td>&quot; + author[sortArray] + &quot;</tr></td>
as there are other arrays which will have to be sorted the same way as in:
table += &quot;<tr><td>&quot; + author[sortArray] + &quot;</td><td>&quot; + title[sortArray] + &quot;</td></tr>&quot;
where &quot;title&quot; is another array.
Doing it like this I don't have to sort all the arrays (more than 5)

What is wrong with the above script since it doesn't sort it correctly?

Thanks for any help/suggestions
/Peter
 
Hi Peter,
You should not use sort as a function name. Sort is a reserved method name.

The following example does what you want:

var author = new Array()
author[0] = &quot;author 5&quot;
author[1] = &quot;author 3&quot;
author[2] = &quot;author 7&quot;
author[3] = &quot;author 4&quot;
author[4] = &quot;author 6&quot;
author[5] = &quot;author 1&quot;
author[6] = &quot;author 8&quot;
author[7] = &quot;author 2&quot;
var dosort=author.sort();
alert(dosort);
 
Hi ChetN,

Thanks for your reply.

using &quot;var dosort=author.sort()&quot; isn't an option for my as I have severel array working together. author[0] is used in connection with title[0], subject[0] etc. So if I rearrange the array I will break the relationships between these arrays.

Any other suggestions?
 
I think I found what the problem is, but am missing a solution. It appears that when calling an array like:

author[sortArray]

where &quot;i&quot; is some number, javascript interprets the number in relation to the alphabet. &quot;a&quot; is 0, &quot;b&quot; is 1 and so on.

If the array author contains:

e, c, g, d, f, a, h, b

Then the array sortArray will, after sorting, contain this sequence of numbers:

5,7,1,3,0,4,2,6

the result of author[sortArray] will then be:

f, h, b, d, a, e, c, g

as 5=f, 7=h and so on, when a=0

But this is not what I want ofcourse. If sortArray = 5 then I want the information contained in the 6th position of the array author which, in this case i &quot;a&quot;.

How do I do that and why does javascript interpret numbered array-positions in relation to the alphabet?

Thanks for any help
/Peter
 
Without intending to, you ARE changing the order in the original author array. By assigning author to tmpArray, you are creating an alias for the same exact array. So first you are changing the order or author, then you are accessing it out of order. If you merely displayed author from 0 to author.length at this point, you would see that it is already sorted. However, since that is not what you want to do, replace:

Code:
tmpArray = author;

with:

Code:
 for(var j=0; j<author.length; j++)
 {
  tmpArray[j] = author[j];
 }

...and all should be right with the world.

--Dave

P.S., please enclose code snippets with [ignore]
Code:
...
[/ignore] tags. Your [ignore]'s[/ignore] are mistaken for calls to italicize which is no bother unless someone wants to copy/paste your code in attempt to find a way to make it work. I did this at first too, until someone pointed it out to me. Click on the &quot;Process TGML&quot; link below for other tags specific to the Tek Tip forum (it opens in a new window, so you will not lose your place).
 
That was it... I spend the entire weekend trying to figure this one out :)

Thanks a lot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top