Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to return a passed string with letters in alphabetical order in JavaScript?
We can apply String split(), Array sort() and Array join() methods to write a function that returns a passed string with letters in alphabetical order in JavaScript. This approach converts the string into an array, sorts the characters, and joins them back into a string.
Input string : tutorialspoint Output string: aiilnooprstttu
Syntax
function sortString(str) {
return str.split("").sort().join("");
}
Steps
-
Split the string into an array of characters using
split("")method. -
Apply the
sort()method to sort the characters in alphabetical order. -
Finally, use the
join("")method to join the characters back into a string.
Example 1: Basic Alphabetical Sorting
The following program sorts the characters in a string in alphabetical order:
<!doctype html>
<html>
<head>
<title>Sort String Alphabetically</title>
</head>
<body>
<div id="result"></div>
<script>
function alphaOrder(str){
var arr = str.split("");
arr.sort();
var sortedStr = arr.join("");
return sortedStr;
}
var result = alphaOrder("tutorialspoint");
document.getElementById("result").innerHTML = "Original: tutorialspoint<br>Sorted: " + result;
</script>
</body>
</html>
Original: tutorialspoint Sorted: aiilnooprstttu
Example 2: Reverse Alphabetical Order
The following program sorts the characters in reverse alphabetical order using the reverse() method:
<!doctype html>
<html>
<head>
<title>Sort String Reverse</title>
</head>
<body>
<div id="result"></div>
<script>
function reverseAlphaOrder(str){
var arr = str.split("");
arr.sort();
arr.reverse();
var sortedStr = arr.join("");
return sortedStr;
}
var result = reverseAlphaOrder("tutorialspoint");
document.getElementById("result").innerHTML = "Original: tutorialspoint<br>Reverse Sorted: " + result;
</script>
</body>
</html>
Original: tutorialspoint Reverse Sorted: utttrsoopniila
Example 3: One-liner Function
A more concise version using method chaining:
<!doctype html>
<html>
<head>
<title>Sort String One-liner</title>
</head>
<body>
<div id="result"></div>
<script>
const sortString = str => str.split("").sort().join("");
var examples = ["hello", "javascript", "programming"];
var results = "";
examples.forEach(function(str) {
results += str + " ? " + sortString(str) + "<br>";
});
document.getElementById("result").innerHTML = results;
</script>
</body>
</html>
hello ? ehllo javascript ? aacijprstv programming ? agimmnoprrg
Key Points
-
The
sort()method sorts elements as strings by default, which works perfectly for alphabetical sorting. -
Case sensitivity matters - uppercase letters come before lowercase in Unicode ordering.
-
For case-insensitive sorting, use
sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())). -
The original string remains unchanged as strings are immutable in JavaScript.
Comparison Table
| Method | Code | Use Case |
|---|---|---|
| Basic Sort | str.split("").sort().join("") |
Simple alphabetical ordering |
| Reverse Sort | str.split("").sort().reverse().join("") |
Reverse alphabetical ordering |
| Case-insensitive | str.split("").sort((a,b) => a.toLowerCase().localeCompare(b.toLowerCase())).join("") |
Mixed case strings |
Conclusion
Using split(), sort(), and join() provides an efficient way to alphabetically sort string characters. This method is simple, readable, and works well for most use cases involving character reordering.
