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] = "author 5"
author[1] = "author 3"
author[2] = "author 7"
author[3] = "author 4"
author[4] = "author 6"
author[5] = "author 1"
author[6] = "author 8"
author[7] = "author 2"
var sortArray = new Array(); //global array
function sort(sortOrder) {
var tmp = "";
//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 = "false";
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 = "true"; //continue iteration
}
}
} while(b == "true"
;
}
function buildTable() {
sort()
var table = "<table>";
for(var i = 0; i < sortArray.length; i++) {
table += "<tr><td>" + author[sortArray] + "</td></tr>";
}//for
table += "</table>";
document.write(table);
}
</script>
</head>
<body>
<script>
buildTable();
</script>
</body>
</html>
END PAGE
I have to make code like this:
table += "<tr><td>" + author[sortArray] + "</tr></td>
as there are other arrays which will have to be sorted the same way as in:
table += "<tr><td>" + author[sortArray] + "</td><td>" + title[sortArray] + "</td></tr>"
where "title" 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
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] = "author 5"
author[1] = "author 3"
author[2] = "author 7"
author[3] = "author 4"
author[4] = "author 6"
author[5] = "author 1"
author[6] = "author 8"
author[7] = "author 2"
var sortArray = new Array(); //global array
function sort(sortOrder) {
var tmp = "";
//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 = "false";
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 = "true"; //continue iteration
}
}
} while(b == "true"
}
function buildTable() {
sort()
var table = "<table>";
for(var i = 0; i < sortArray.length; i++) {
table += "<tr><td>" + author[sortArray] + "</td></tr>";
}//for
table += "</table>";
document.write(table);
}
</script>
</head>
<body>
<script>
buildTable();
</script>
</body>
</html>
END PAGE
I have to make code like this:
table += "<tr><td>" + author[sortArray] + "</tr></td>
as there are other arrays which will have to be sorted the same way as in:
table += "<tr><td>" + author[sortArray] + "</td><td>" + title[sortArray] + "</td></tr>"
where "title" 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