Array and sorting demo I
Data to sort
Note: This sort requires numbers to sort properly.
The JavaScript code:
function sortme() {
var fields = document.getElementsByTagName("INPUT");
var nums = new Array();
for (var i=0; i<fields.length; i++) nums.push(parseFloat(fields[i].value));
for (var i=0; i<nums.length-1; i++) {
for (var j=i+1; j<nums.length; j++) {
if (nums[i] > nums[j]) {
var tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
}
for (var i=0; i<nums.length; i++) fields[i].value=nums[i];
return false;
}