JavaScript Sort an Array of Strings (original) (raw)

Last Updated : 03 Dec, 2024

Here are the various methods to sort an array of strings in JavaScript

1. Using Array.sort() Method

The sort() method is the most widely used method in JavaScript to sort arrays. By default, it sorts the strings in lexicographical (dictionary) order based on Unicode values.

JavaScript `

let a = ['Banana', 'Apple', 'Cherry', 'Mango']; a.sort(); console.log(a);

`

Output

[ 'Apple', 'Banana', 'Cherry', 'Mango' ]

2. Using Array.sort() with a Custom Comparator

If you need more control over the sorting (e.g., sorting case-insensitively or in reverse order), you can pass a custom comparator function to the sort() method.

**Case-Insensitive Sort

JavaScript `

let a = ['banana', 'Apple', 'cherry', 'Mango']; a.sort((s1, s2) => s1.toLowerCase().localeCompare(s2.toLowerCase())); console.log(a);

`

Output

[ 'Apple', 'banana', 'cherry', 'Mango' ]

**Reverse Order Sort

JavaScript `

let a = ['Banana', 'Apple', 'Cherry', 'Mango']; a.sort((s1, s2) => s2.localeCompare(s1)); console.log(a);

`

3. Using Array.sort() with Numeric Sorting (for Length)

If you need to sort an array of strings by the length of each string (instead of lexicographically), you can customize the comparator to compare string lengths.

JavaScript `

let a = ['Banana', 'Apple', 'Cherry', 'Mango']; a.sort((s1, s2) => s1.length - s2.length); console.log(a);

`

Output

[ 'Apple', 'Mango', 'Banana', 'Cherry' ]

4. Using Array.sort() with Locale-Sensitive Sorting

If you're working with strings in different languages and need to account for locale-specific sorting rules (e.g., accented characters), you can use localeCompare() with the locales and options parameters.

JavaScript `

let a = ['apple', 'banana', 'cherry', 'mango', 'ápple']; a.sort((s1, s2) => s1.localeCompare(s2, 'en', { sensitivity: 'base' })); console.log(a);

`

Output

[ 'apple', 'ápple', 'banana', 'cherry', 'mango' ]

5. Using Array.sort() with Custom Order (Non-Alphabetical)

If you want to sort the strings based on a custom order (not just alphabetically), you can define an order map and sort the array based on this custom order.

JavaScript `

let a = ['Banana', 'Apple', 'Mango', 'Cherry']; let order = ['Mango', 'Apple', 'Banana', 'Cherry']; //custom order a.sort((s1, s2) => order.indexOf(s1) - order.indexOf(s2)); console.log(a);

`

Output

[ 'Mango', 'Apple', 'Banana', 'Cherry' ]

Which Approach to Choose?

**Method **When to Use **Why Choose It
**sort() **Method For simple alphabetical sorting. Default and efficient for most cases, with lexicographical sorting.
**Custom Comparator with sort() When you need case-insensitive or reverse order sorting. Provides full control over how strings are compared and ordered.
**sort() **with Length Sorting When sorting by string length. Useful when you need to prioritize short or long strings.
**localeCompare() **for Locale-Sensitive Sorting When dealing with internationalization and accented characters. Ensures correct sorting for different languages and special characters.
**Custom Order Sorting When you need a custom sorting order. Great for non-alphabetical sorting based on predefined rules.