Array.prototype.toSortedintermediateconst nums = [5, 2, 8, 1]
nums.toSorted((a, b) => b - a) // β [8, 5, 2, 1]Pass a comparator to toSorted() to control the sort order without mutating the original.
You can pass any comparator function to toSorted, just like sort.
Implement sortByLength which returns a copy of the string array sorted by string length (shortest first).