<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function sort_start(form, jun_value) {
nanExists = false;
inputString = form.numbers.value;
inputNumbers = inputString.split(",");
for (var i = 0; i < inputNumbers.length; i++) {
inputNumbers[i] = parseInt(inputNumbers[i], 10);
if (isNaN(inputNumbers[i])) {
nanExists = true;
break;
}
}
inputNumbers = mov_sort(inputNumbers, 0, inputNumbers.length - 1, jun_value);
if (nanExists)
alert("数字の区切りにカンマ , を入れてください。");
else
form.answers.value = dsp_result(inputNumbers, 0);
}
function dsp_result(inputArray, num) {
if ((inputArray.length - 1) >= num)
return (inputArray[num] + "," + dsp_result(inputArray,(num + 1)));
else return "";
}
function mov_sort(inputArray, start, rest, jun_value) {
for (var i = rest - 1; i >= start; i--) {
for (var j = start; j <= i; j++) {
if (jun_value == 'up') {
if (inputArray[j+1] < inputArray[j]) {
var tempValue = inputArray[j];
inputArray[j] = inputArray[j+1];
inputArray[j+1] = tempValue;
}
} else {
if (inputArray[j+1] > inputArray[j]) {
var tempValue = inputArray[j];
inputArray[j] = inputArray[j+1];
inputArray[j+1] = tempValue;
}
}
}
}
return inputArray;
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<center>
<form>
<table border=1 cellpadding=6>
<tr>
<td align=center><font color=blue><b>数字列入力欄</b></font><br>
<input type=text name=numbers size=70 value="37,9,8,91,5,2,6,7,33,1,45,21,5,6,4,4,2,5,71,7,13,8,51,1,8,4"><br>
<font color=darkcyan size=2>カンマで区切った数字を入力して下さい。
</td>
</tr>
<tr>
<td colspan=2 align=center>
<input type=button value="昇順ソート" onClick="sort_start(this.form, 'up')">
<input type=button value="降順ソート" onClick="sort_start(this.form, 'down')">
</td>
</tr>
<tr>
<td align=center><font color=blue><b>ソート結果</b></font><br>
<input type=text name=answers size=70></td>
</tr>
</table>
</form>
</center>
</BODY>
</HTML>
|