Is javascript bubble sort the same as .sort((a,b) => a-b
? Is it more efficient? Why? It seems the same, what is the difference? If different, when would I use bubble sort, when would .sort
be best and why?
Say for use in a massive photo library using dates, what sorting method is the best to use in javascript?
I have never tried bubble sort, this (below) is on fcc, and I thought, what is the difference? I have never seen this used and .sort((a,b) => a-b
does this, no?
I did read through all of the sort vs bubble sort on here asked before, they were mostly in different languages I also read the MDN docs and still wanted more clarity.
from freeCodeCamp
const bubbleSort = (arr) => {
let swapped;
do {
swapped = false;
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
let temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = true;
}
}
} while (swapped);
return arr;
};