GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||
GeezerVison is showing flatcoding.com — JavaScript toSorted Function Guide with Examples - FlatCoding Home JavaScript toSorted Function Guide with Examples
Table of ContentThis guide shows how toSorted function works in JavaScript with arrays. It covers syntax, rules, and examples for numbers, text, and objects within ES2023 support. Understand the
|
| Method | Changes Original Array | Returns New Array |
|---|---|---|
| sort() | Yes | No |
| toSorted() | No | Yes |
Use sort() when you want to change the array itself and the toSorted() when you want a new array without changes to the original.
Sort Numbers in Ascending Order:
const nums = [10, 5, 8, 1];
const sorted = nums.toSorted((a, b) => a - b);
console.log(sorted);This shows how to sort numbers in ascending order while the original array stays the same. It returns a new array with ordered numbers.
Sort Strings by Length:
const items = ["banana", "kiwi", "apple"];
const sorted = items.toSorted((a, b) => a.length - b.length);
console.log(sorted);This shows how to sort text by length. It returns a new array with text in order by length. The original array stays the same.
Sort Objects by Age:
const people = [{age:30},{age:20},{age:40}];
const sorted = people.toSorted((a, b) => a.age - b.age);
console.log(sorted);This shows how to sort objects by a property. It creates a new array in order of age and does not change the original array.
Sort with Custom Compare Function:
const letters = ["b", "C", "a"];
const sorted = letters.toSorted((a, b) => a.localeCompare(b, undefined, {sensitivity: 'base'}));
console.log(sorted);This shows how to sort values with a custom compare rule. It gives a new array with values sorted without case issues and leaves the old array untouched.
You learned how toSorted creates a new array and leaves the original untouched.
Here is a quick recap:
sort() changes the array itself. toSorted() makes a new sorted array.
const numbers = [3, 1, 4, 2];
const sorted = numbers.toSorted();
console.log(sorted); // [1, 2, 3, 4]
console.log(numbers); // [3, 1, 4, 2]
The difference is that sort() changes the original array while toSorted() creates a new one.
This makes toSorted safer in most use cases.
const fruits = ["banana", "apple", "mango"];
const s1 = fruits.sort();
console.log(fruits); // ["apple", "banana", "mango"]
const fruits2 = ["banana", "apple", "mango"];
const s2 = fruits2.toSorted();
console.log(fruits2); // ["banana", "apple", "mango"]
console.log(s2); // ["apple", "banana", "mango"]
You can pass a compare function inside toSorted() to sort objects by a property.
This does not affect the original array of objects.
const users = [
{name: "Alice", age: 25},
{name: "Bob", age: 20},
{name: "Charlie", age: 30}
];
const sortedUsers = users.toSorted((a, b) => a.age - b.age);
console.log(sortedUsers);
/*
[
{name: "Bob", age: 20},
{name: "Alice", age: 25},
{name: "Charlie", age: 30}
]
*/
The toSorted function is part of ES2023 and modern browsers support it. Older browsers may not support it without a polyfill.
Math.exp is a built-in JavaScript function that returns the value of e raised to a given number. Here, e is…
Math.atan() function in JavaScript helps you to solve math problems in your code. It turns a number into an angle.…
JavaScript moves fast, and new features come. Some browsers do not support these features and cause issues for users. Developers…
If you are ready to take your first steps into the realm of web development, then there cannot be a…
JavaScript added Math.cbrt to solve cube roots. Before that, developers used custom code or Math.pow with 1/3. It works with…
The development of full-featured and interesting pages cannot be done without JavaScript which makes it possible to animate plain HTML…
JavaScript Symbol Type gives a way to make unique values. A Symbol never matches any other value and this helps…
Click a button on a website. Something changes. A menu opens or a message pops up. That’s how JavaScript works.…
JavaScript optional chaining lets you access object values without runtime errors. It checks if a property exists before access and…
Bitwise operators work with binary data at the bit level in JavaScript. They process numbers in binary form and return…