- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to return a passed string with 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.
- Related Articles
- Removing n characters from a string in alphabetical order in JavaScript
- C program to sort names in alphabetical order with string functions.
- Move string capital letters to front maintaining the relative order - JavaScript
- Finding the power of a string from a string with repeated letters in JavaScript
- Python – Extract Strings with Successive Alphabets in Alphabetical Order
- Sorting numbers in ascending order and strings in alphabetical order in an array in JavaScript
- Check if the characters of a given string are in alphabetical order in Python
- Check whether the vowels in a string are in alphabetical order or not in Python
- For Hosts Locale how to convert a string to lowercase letters in JavaScript?
- C program to sort names in alphabetical order
- How to get the maximum count of repeated letters in a string? JavaScript
- For the Hosts Locale how to convert a string to uppercase letters in JavaScript?
- How to sort an R data frame rows in alphabetical order?
- Interchanging first letters of words in a string in JavaScript
- Sort the array of strings according to alphabetical order defined by another string in C++
