- ✕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 - W3Schools
Array.prototype.sort () - JavaScript | MDN
Jul 20, 2025 · The sort() method of Array instances sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting …
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.
Sort An Array Of Arrays In JavaScript - GeeksforGeeks
Jul 23, 2025 · To sort an array of arrays by a specific column, you need to provide a custom comparator function to sort () that specifies which index in the sub-arrays should be compared.
javascript - Sorting an array of objects by property values - Stack ...
While it is a bit of an overkill for just sorting a single array, this prototype function allows to sort Javascript arrays by any key, in ascending or descending order, including nested keys, using dot syntax.
- Reviews: 1
- Watch full videoWatch full video
JavaScript's sort()method: From basics to custom …
Mar 7, 2025 · Discover how to sort arrays in JavaScript using sort () and toSorted (), including customization and language-sensitive sorting techniques.
How to Sort a JavaScript Array Based on Another Array (Without IDs ...
Jan 16, 2026 · Sorting arrays is a fundamental task in JavaScript, but what if you need to sort one array based on the order defined by another array —and you don’t have unique IDs to map elements?
How to Sort Arrays and Arrays of Objects in JavaScript
JavaScript offers various methods to sort arrays, allowing you to modify the original array, create a sorted copy, or apply custom sorting logic. This guide explores different techniques for sorting both …
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: Methods for Ordering Array …
Aug 21, 2024 · In this comprehensive guide, we'll explore the ins and outs of sorting arrays in JavaScript. We'll cover built-in methods, custom sorting functions, and …