We can sort the strings in JavaScript by following methods described below:
- Using sort() method
- Using loop
Using sort() method: In this method, we use predefined sort() method of JavaScript to sort the array of string. This method is used only when the string is alphabetic. It will produce wrong results if we store numbers in an array and apply this method.
Examples:
Original String
Suraj, Sanjeev, Rajnish, Yash, Ravi
After sorting
Rajnish, Ravi, Sanjeev, Suraj, YashOriginal String
40, 100, 1, 5, 25, 10
After sorting
1, 10, 100, 25, 40, 5
Below program illustrates the above approach:
Program:
<script> // JavaScript code to sort strings // Original string var string = ["Suraj", "Sanjeev", "Rajnish", "Yash", "Ravi"]; // Print original string array document.write("Original String</br>"); document.write(string); document.write("</br>"); // Use sort() method to sort the strings string.sort(); document.write("</br>After sorting</br>"); // Print sorted string array document.write(string); </script> |
Output:
Original String Suraj, Sanjeev, Rajnish, Yash, Ravi After sorting Rajnish, Ravi, Sanjeev, Suraj, Yash
Using loop: We will use a simple approach of sorting to sort the strings. In this method, we will use a loop and then compare each element and put the string at its correct position. Here we can store numbers in an array and apply this method to sort the array.
Examples:
Original String
Suraj,Sanjeev,Rajnish,Yash,Ravi
After sorting
Rajnish,Ravi,Sanjeev,Suraj,YashOriginal String
40, 100, 1, 5, 25, 10
After sorting
1,5,10,25,40,100
Below program illustrates the above approach:
Program:
<script> // JavaScript code to sort the strings // Function to perform sort function string_sort(str) { var i = 0, j; while (i < str.length) { j = i + 1; while (j < str.length) { if (str[j] < str[i]) { var temp = str[i]; str[i] = str[j]; str[j] = temp; } j++; } i++; } } // Driver code // Original string var string = ["Suraj", "Sanjeev", "Rajnish", "Yash", "Ravi"]; // Print original string array document.write("Original String</br>"); document.write(string); document.write("</br>"); // Call string_sort method string_sort(string); document.write("</br>After sorting</br>"); // Print sorted string array document.write(string); </script> |
Output:
Original String Suraj, Sanjeev, Rajnish, Yash, Ravi After sorting Rajnish, Ravi, Sanjeev, Suraj, Yash
Recommended Posts:
- JavaScript | Replace multiple strings with multiple other strings
- How to sort a list in C# | List.Sort() Method Set -1
- How to sort an Array in C# | Array.Sort() Method Set – 2
- PHP | Sort array of strings in natural and standard orders
- How to use Array Reverse Sort Functions for Integer and Strings in Golang?
- JavaScript Array sort() Method
- Merge Sort for Linked Lists in JavaScript
- JavaScript | Sort() method
- JavaScript | typedArray.sort() with Examples
- Sort an array of objects using Boolean property in JavaScript
- Sort array of objects by string property value in JavaScript
- Sort an Object Array by Date in JavaScript
- How to sort rows in a table using JavaScript?
- How to sort an array of object by two fields in JavaScript ?
- How to Sort/Order keys in JavaScript objects ?
- How to sort element by numerical value of data attribute using JavaScript ?
- How to Sort Numeric Array using JavaScript ?
- How to sort an array on multiple columns using JavaScript ?
- JavaScript TypeError - Invalid Array.prototype.sort argument
- JavaScript | Strings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


