Sort a string in JavaScript
This post will discuss how to sort a string in JavaScript.
There are several ways to sort a string in JavaScript, depending on how we want to order the characters in the string. Sorting a string means arranging the characters in a certain sequence, such as alphabetically, numerically, or by length. Here are some of the most common and easy-to-use functions for sorting a string in JavaScript:
1. Using Array.sort() function
This is a simple and straightforward way to sort an array of strings alphabetically by default. To use it, we need to first convert the string to an array of characters using the split() function, then sort the array using the Array.sort() function, and finally join the array back to a string using the join() function. The sort() function orders the characters based on their Unicode values, which may not always match the alphabetical order. For example, if we want to sort the string "Hello, World!" alphabetically, we can write:
|
1 2 3 4 5 |
let str = "Hello, World!"; let sorted = str.split("").sort().join(""); console.log(sorted); // " !,HWdellloor" |
2. Using localeCompare() function
This is a more advanced and accurate way to sort a string by comparing the characters based on their language-specific rules. The localeCompare() function returns a negative number if the first string comes before the second string, a positive number if the first string comes after the second string, or zero if they are equal. To use it, we need to first convert the string to an array of characters, sorting the array using the sort() function with a callback function that uses the localeCompare() function, and then joining the array back into a string. For example, if we want to sort the string "Hello, World!" using the english locale, we can write:
|
1 2 3 4 5 |
let str = "Hello, World!"; let sorted = str.split("").sort((a, b) => a.localeCompare(b, "en")).join(""); console.log(sorted); // " ,!deHllloorW" |
3. Using custom comparison functions
This is a more flexible and creative way to sort a string by defining our own rules for comparing and ordering the characters. The comparison function should take two parameters that represent two characters, and return a negative number if the first character comes before the second character, a positive number if the first character comes after the second character, or zero if they are equal. To use it, we need to first convert the string to an array of characters, then sort the array using the sort() function with our custom comparison function, and finally join the array back to a string. For example, if we want to sort the string "Hello, World!" by their custom weights, we can write:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// A function that returns the custom weights of characters const charWeight = (char) => { let names = { " ": 5, "!": 8, "H": 4, "e": 1, "l": 3, "o": 2, "W": 5, "r": 6, "d": 4, ",": 7 }; return names[char]; }; let str = "Hello, World!"; let sorted = str.split("").sort((a, b) => { // Write our own logic here // For example, compares two characters by weight return charWeight(a) - charWeight(b) }).join(""); console.log(sorted); // "eoolllHd Wr,!" |
That’s all about sorting a string in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)