Quote:
Originally Posted by AndrewGSW
If you want to sort without using sort() then you will need to implement your own sorting method.
|
For example:-
Code:
<script type = "text/javascript">
function bubbleSort(arrayToSort) {
for (var i = 0; i < len-1; i++) {
var t1 = arrayToSort[i+1].toString();
t1 = t1.replace(/\D/g,"")*1;
var t2 = arrayToSort[i].toString();
t2 = t2.replace(/\D/g,"")*1;
if (t1 > t2) {
var temp = arrayToSort[i+1];
arrayToSort[i+1] = arrayToSort[i];
arrayToSort[i] = temp;
}
}
}
function bubbleTest(arrayToSort) {
var count = 0;
while (count < len-1) {
bubbleSort(arrayToSort);
count ++;
}
document.write('Iterations taken: ' + count + '<br>');
document.write('The sorted array is now ' + arrayToSort + '<br>');
}
var arrayToSort = ["DOWNc18","OUTc11","INc21","UPc0","SIDEc33", "FIG99", "GY43x", "KHc25"];
var len = arrayToSort.length;
document.write('The original array was ' + arrayToSort + '<br>');
bubbleTest(arrayToSort);
</script>