I've tried to use bubble sort in javascript, but stuck up while swapping the numbers. The idea is to group the numbers(eg: input: 154514, output should be : 114455). So I tried to use bubble sort to get the above output.
My code is here:
function numSort(num){
var temp = '';
var temp1 = '';
arr = num.toString();
var n = arr.length;
for(i=0; i<n-1; i++){
for(d=0; d<n-i-1; d++){
if(arr[d] > arr[d+1]){
temp = arr[d];//here I'm trying to swap the inputs as: 89, but it's not letting.
arr[d] = arr[d+1];
arr[d+1] = temp;
}console.log(arr[d]);
}
}
}console.log(numSort(98));
The swapping is not working. Kindly help.
Thanks so much in advance.