
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return String Letters in Alphabetical Order in JavaScript
We could 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. A JavaScript function is a block of code that is executed when it is invoked. A string is a sequence of characters. In JavaScript, strings are represented by the type String.
Input string : tutorialspoint Output string: aiilnooprstttu
Steps
Split the string into an array of characters using split() method.
Apply the sort() method to sort the characters in alphabetical order.
Finally, we use the join() method to join the characters back into a string.
The Array.sort() method sorts the elements of an array in place and returns the array. The sort is not necessarily stable. The default sort order is according to string Unicode code points.
Example 1
The following program sorts the characters in a string in alphabetical order
<!doctype html> <html> <head> <title>Examples</title> </head> <body> <div id="result"></div> <script> function alphaOrder(str){ var arr = str.split(""); arr.sort(); var sortedStr = arr.join(""); return sortedStr } document.getElementById("result").innerHTML = alphaOrder("tutorialspoint") </script> </body> </html>
Explanation
In the above program, we have defined a string str with the value "tutorialspoint". We have used the split() method to split the string into an array of characters. We have used the sort() method to sort the characters in alphabetical order. Finally, we have used the join() method to join the characters back into a string.
We can also use the Array.sort() method to sort the characters in a string in reverse alphabetical order.
Example 2
The following program sorts the characters in a string in reverse alphabetical order
<!doctype html> <html> <head> <title>Examples</title> </head> <body> <div id="result"></div> <script> function alphaOrder(str){ var arr = str.split(""); arr.sort(); arr.reverse() var sortedStr = arr.join(""); return sortedStr } document.getElementById("result").innerHTML = alphaOrder("tutorialspoint") </script> </body> </html>
In the above code, we have used the reverse() method to reverse the array.
Advantages
Using the Array.sort() method to sort the characters in a string has the following advantages
It is easy to understand and write.
It is efficient in terms of time and space complexity.
Disadvantages
Some of the disadvantages of using the Array.sort() method to sort the characters in a string are as follows
The sorting is not necessarily stable.
The default sort order is according to string Unicode code points.
There are other methods that can be used to sort the characters in a string in alphabetical order such as the following
The String.prototype.localeCompare() method
The String.prototype.charCodeAt() method
Conclusion
In this tutorial, we discussed how to write a function that returns a passed string with letters in alphabetical order in JavaScript. We saw how to use the Array sort() method to sort the characters in a string in alphabetical order. We also saw how to use the Array sort() method to sort the characters in a string in reverse alphabetical order. Finally, we saw some of the advantages and disadvantages of using the Array sort() method to sort the characters in a string.