The following code should convert a string into an array of numbers and sort them in descending order. The purpose is to find a substitution for the sort() method.
Something is wrong. If 7 is placed in the first half of the array (like in the example), the code does not work properly. If you swap 7 for a number bigger than the last one (22 in the example), the code will work fine.
I'm looking to get it to work right regardless of the positioning of the numbers.
var row = '92 43 7 119 51 22';
var row = row.split(' ');
var column = row.map(Number);
function arrangeNum(column) {
for (var i = 0; i <= column.length - 1; i++) {
for (var j = column.length - i; j >= 0; j--) {
if (column[j] > column[j - 1]) {
var temp = column[j];
column[j] = column[j - 1];
column[j - 1] = temp;
}
}
}
return column;
}
console.log(arrangeNum(column));
