我在尝试对为作业创建的书单进行排序时遇到问题。书籍的标题应该随着日期移动,并在单击按钮后从最旧到最新排序。但是,我不知道为什么点击按钮后唐吉诃德和白鲸没有出现。有什么我遗漏或做错了吗?帮助表示赞赏。
var names = new Array("To Kill a Mockingbird","Jaws","Don Quixote","Moby-Dick","Northern Lights");
var published = new Array("July 11, 1960", "February 1, 1974","July 11, 1615","October 18, 1851","November 12, 1995");
function display()
{
for(var i = 0; i < names.length; i++)
{
var temp = "cell" + i + "0";
document.getElementById(temp).innerHTML = names[i];
temp = "cell" + i + "1";
document.getElementById(temp).innerHTML = published[i];
}
}
function SortIt( )
{
var oldest = 0;
var index = 0;
var j = 0;
var k = 0;
var count = 0;
var temp = 0; var temp2 = "";
count = published.length;
count = names.length;
for (k =0; k <= (count -1); k++ )
{
oldest = Date.parse(published[k]);
index = k;
for (j = (k + 1); j <= (count-1); j++)
{
if (Date.parse(published[j]) < oldest )
{
oldest = Date.parse(published[j]);
index = j;
}
}
if(k != index)
{
temp = published[k];
published[k] = published[index];
published[index] = temp;
temp2 = names[k];
names[k] = index[k];
names[index] = temp2;
}
}
display();
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Program 9 - Selection Sort</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="Carbajal_Lab9.js"></script>
</head>
<body onload="display()">
<h1>Program 9 - Selection Sort</h1>
<table id ="workTable" border ="2">
<tr>
<td> <span id = "cell00"/> </td>
<td> <span id = "cell01"/> </td>
</tr>
<tr>
<td> <span id = "cell10"/> </td>
<td> <span id = "cell11"/> </td>
</tr>
<tr>
<td> <span id = "cell20"/> </td>
<td> <span id = "cell21"/> </td>
</tr>
<tr>
<td> <span id = "cell30"/> </td>
<td> <span id = "cell31"/> </td>
</tr>
<tr>
<td> <span id = "cell40"/> </td>
<td> <span id = "cell41"/> </td>
</tr>
</table>
<form>
<input type="button" value="Sort" onclick="SortIt()"/>
</form>
</body>
</html>