I am trying to wrap my head around sorting in Javascript when it comes to strings. Ok so i have this function here:
var numericalOrder = function(array){
if(arguments.length === 0 || !Array.isArray(array)){
throw new Error();
}
var anyChange;
for(var i = 0; i < array.length - 1; i++){
anyChange = false;
for(var x = 0; x < array.length - 1; x++){
if(array[x] > array[x + 1]){
anyChange = true;
var temp = array[x];
array[x] = array[x + 1];
array[x + 1] = temp;
}
}
if(!anyChange){
return array;
}
}
return array;
};
When given an array of numbers the function will arrange the values in numerical order, but what i'm confused about is how this same function is able to alphabetize an array of strings. I know there is an array sort() method in javascript but I'm trying to fully grasp this concept. Any help is appreciated :)!