- ✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links.
The sort() method in JavaScript sorts the elements of an array in place and returns the sorted array. By default, it sorts the elements as strings in ascending order.
Example: Sorting Strings
const fruits = ["Banana", "Orange", "Apple", "Mango"];fruits.sort();console.log(fruits); // ["Apple", "Banana", "Mango", "Orange"]Copied!✕CopySorting Numbers
To sort numbers correctly, you need to provide a compare function:
const points = [40, 100, 1, 5, 25, 10];points.sort((a, b) => a - b);console.log(points); // [1, 5, 10, 25, 40, 100]Copied!✕CopyThe compare function (a, b) => a - b sorts the numbers in ascending order.
Sorting Objects
You can also sort an array of objects by a specific property:
const items = [{ name: "Edward", value: 21 },{ name: "Sharpe", value: 37 },{ name: "And", value: 45 },{ name: "The", value: -12 },{ name: "Magnetic", value: 13 },{ name: "Zeros", value: 37 }];items.sort((a, b) => a.value - b.value);console.log(items);Copied!✕CopyThis will sort the objects by their value property.
Important Considerations
JavaScript Array sort () Method - W3Schools
Description The sort() method sorts the elements of an array. The sort() method sorts the elements as strings in alphabetical and ascending order. The sort() method overwrites the original array.
JavaScript Array sort: Sorting an Array More Effectively
This tutorial shows you how to use the JavaScript array sort method to sort arrays of numbers, strings, and objects.
JavaScript Array sort () Method - GeeksforGeeks
Jan 16, 2026 · The sort () method in JavaScript is used to arrange array elements in a specific order, usually alphabetically or in ascending order. It modifies the original …
JavaScript's sort()method: From basics to custom sorting …
Mar 7, 2025 · Discover how to sort arrays in JavaScript using sort () and toSorted (), including customization and language-sensitive sorting techniques.
Searches you might like
JavaScript - Array sort () Method - Online Tutorials Library
The JavaScript Array.sort () function is used to sort the elements of an array. By default, this method converts array elements into strings and then sorts them based on their UTF-16 code unit values.
JavaScript Sort Array - How to Sort an Array Accurately
Mar 14, 2023 · In this article, you will learn how to sort an array in JavaScript, some shortcomings associated with the sort() method, and how to get more accurate results. If you are in a rush, the best …
JavaScript Array sort () - Programiz
Since all non-undefined elements are converted to strings before sorting them, we cannot sort numbers using their numeric value by default. Let's see how we can implement this using a custom function.
JavaScript sort - sorting arrays in JS - ZetCode
Apr 4, 2025 · JavaScript sort tutorial shows how to sort arrays in JavaScript. The tutorial provides numerous examples to demonstrate array sorting in JS.
JavaScript Array.sort () Method - Tutorial Kart
The Array.sort() method in JavaScript is used to sort the elements of an array in place and return the sorted array. By default, the elements are converted to strings and sorted in ascending order based on …