toSorted < Array < JavaScript
The toSorted() method of an array returns a new array with the elements of the original array sorted. Unlike sort(), it doesn't affect the original array, it copies it. Like sort(), you can pass a custom sort function to it if you need to sort the elements in a way different than the default ascending.
const names = ['Vic', 'Andy', 'Zack', 'Beth'];
const sortedNames = names.toSorted();
console.log(sortedNames);
// ["Andy", "Beth", "Vic", "Zack"]
console.log(names);
// ["Vic", "Andy", "Zack", "Beth"]
const names1 = ['Vic', 'Andy', 'Zack', 'Beth'];
const sortedNames1 = names1.toSorted((a, b) => (a > b ? -1 : 1));
console.log(sortedNames1);
// ["Zack", "Vic", "Beth", "Andy"]
const numbers = [1, 10, 21, 2];
const sortednumbers = numbers.toSorted((a, b) => a - b);
console.log(sortednumbers);
// [1, 2, 10, 21]
console.log(numbers);
// [1, 10, 21, 2]
const numbers1 = [1, 10, 21, 2];
const sortednumbers1 = numbers.toSorted((a, b) => (b - a));
console.log(sortednumbers1);
// [21, 10, 2, 1]