當點擊OK時,我的代碼會生成一個二維矩陣。代碼將數據存儲在多維數組中,但我試圖向代碼中添加一個排序函數,該代碼使用第4個文本框以升序重新排列代碼。有關如何做到這一點的任何建議?如何按升序對多維數組輸出進行排序?
HTML代碼
<div class="rightDiv">
<div id = "pastcalcblock">
<h3> PAST CALCULATIONS </h3>
<input type = "text" size = "1" id = "text1"/>
<input type = "text" size = "1" id = "text2"/>
<input type = "text" size = "1" id = "text3"/>
<input type = "text" size = "1" id = "text4"/><br>
<input type = "button" value = "Ok" id = "operation" onClick = "display()"/>
<div id = "resultTab">
SORT<br>
<input type = "button" value = "As Entered" id = "enteredBut">
<input type = "button" value = "By Result" id = "resultBut" onClick = "sort()"><br><br>
<div id="expressions">
</div>
</div>
</div>
JavaScript代碼
function display()
{
var arrayOne =[document.getElementById('text1').value,document.getElementById('text2').value,document.getElementById('text3').value,document.getElementById('text4').value ];
new_array=arrayOne.join(" ");
var para = document.createElement("p");
var t = document.createTextNode(new_array);
para.appendChild(t)
document.getElementById("expressions").appendChild(para);
}
function sort(){
var dispArr = [document.getElementById('text1').value,
document.getElementById('text2').value, document.getElementById('text3').value,document.getElementById('text4').value];
var myArray = [];
for(var i = 0 ; i < 1 ; i ++) {
myArray[i] = [];
for(var j = 0 ; j < 5 ; j++)
{
myArray[i][j] = dispArr[j];
console.log(myArray[i][j]);
}
}
}
什麼勁兒行的'myArray'目的:雖然你有一個循環,它僅包含一行。你想要對這一行進行排序? – trincot
好吧,我試圖將數組存儲在另一個數組中,然後運行for循環來比較這些值。 –
我很困惑如何解決這個問題。但我想讓它工作。 –