View Single Post
Old 12-28-2012, 05:38 PM   PM User | #9
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by AndrewGSW View Post
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>
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 12-28-2012 at 07:21 PM.. Reason: Noticed typo
Philip M is offline   Reply With Quote