Array.prototype.sortbeginnerconst words = ['banana', 'apple', 'cherry']
words.sort() // β ['apple', 'banana', 'cherry']Call sort() without arguments to sort strings in ascending Unicode (alphabetical) order.
Array.prototype.sort(compareFn?) sorts elements in place and returns the sorted array. Without a comparator, elements are converted to strings and sorted lexicographically.
β οΈ
sort()mutates the original array. Useslice().sort()to sort a copy.
Challenge: Implement sortAlpha(words) that returns a sorted copy without mutating the original.
sortAlpha(['banana', 'apple', 'cherry']) // β ['apple', 'banana', 'cherry']