javascript sort array without changing original code example
Example 1: sort array without changing original array
let sorted = [].concat(arr).sort(function(a, b) {
return a - b;
})
Example 2: how to sort array without using sort method in javascript
function bubbleSort(array) {
var done = false;
while (!done) {
done = true;
for (var i = 1; i < array.length; i += 1) {
if (array[i - 1] > array[i]) {
done = false;
var tmp = array[i - 1];
array[i - 1] = array[i];
array[i] = tmp;
}
}
}
return array;
}
var numbers = [12, 10, 15, 11, 14, 13, 16];
bubbleSort(numbers);
console.log(numbers);