Very common requirement developers encounter while working with javascript arrays is to sort an array of strings. This article demonstrates easy ways to sort array of strings alphabetically in ascending as well as descending order using various example illustrations.
Table of Contents:
Sort an array of strings using sort()
Javascript’s sort() method is used to sort all the array elements and return the sorted array.
Example:-
Sort the array of names [“George”, “Veronica”, “Mounika”, “Shaunik”]
Using Default Sorting Ascending:-
Code:-
let arrayOfNames = ["George", "Veronica", "Mounika", "Shaunik"]; console.log("Before Sorting: " + arrayOfNames ); console.log("After Sorting: " + arrayOfNames.sort());
Output:-
Frequently Asked:
- Javascript: check if an array includes a value (6 ways)
- Loop through an array in javascript (6 ways)
- Javascript: Copy an array items to another
- Javascript: Sort an Array of Objects by Property Value
Before Sorting: George,Veronica,Mounika,Shaunik After Sorting: George,Mounika,Shaunik,Veronica
Explanation:-
Here, in the above code, we use the sort() method provided by javascript to sort the arrays. The default sorting order is ascending; therefore, we need not provide a custom sort approach.
Using Compare Sorting Descending:-
Code:-
let arrayOfNames = ["George", "Veronica", "Mounika", "Shaunik"]; console.log("Before Sorting: " + arrayOfNames ); // sort your own way descending arrayOfNames.sort((firstElement, secondElement) => { if (firstElement > secondElement) { return -1; } if (firstElement < secondElement) { return 1; } return 0; } ); console.log("After Sorting Descending: " + arrayOfNames);
Output:-
Before Sorting: George,Veronica,Mounika,Shaunik After Sorting Descending: Veronica,Shaunik,Mounika,George
Explanation:-
Here, in the above code, we use the sort() method with a compare function, passed as an argument. Within the compare function we are using a descending way of comparing elements. Hence, the array of strings is sorted descending.
Sort an array of strings implementing custom method
Example:-
Sort the array of names [“George”, “Veronica”, “Mounika”, “Shaunik”]
Using Default Sorting Ascending:-
Code:-
function sortArrayOfStrings(_stringArray) { var i = 0, k; while (i < _stringArray.length) { k = i + 1; while (k < _stringArray.length) { if (_stringArray[k] < _stringArray[i]) { var temp = _stringArray[i]; _stringArray[i] = _stringArray[k]; _stringArray[k] = temp; } k++; } i++; } return _stringArray; } let arrayOfNames = ["George", "Veronica", "Mounika", "Shaunik"]; console.log("Before Sorting: " + arrayOfNames ); // usage of the function console.log("After Sorting: " + sortArrayOfStrings(arrayOfNames));
Output:-
Before Sorting: George,Veronica,Mounika,Shaunik After Sorting: George,Mounika,Shaunik,Veronica
Explanation:-
Here, in the above code, we write our sort approach in the function the sortArrayOfStrings(). Within this function, all the array elements are traversed and sorted by rearranging them using a temp variable.
Using Compare Sorting Descending:-
Code:-
function sortArrayOfStrings(_stringArray) { var i = 0, k; while (i < _stringArray.length) { k = i + 1; while (k < _stringArray.length) { if (_stringArray[k] > _stringArray[i]) { var temp = _stringArray[i]; _stringArray[i] = _stringArray[k]; _stringArray[k] = temp; } k++; } i++; } return _stringArray; } let arrayOfNames = ["George", "Veronica", "Mounika", "Shaunik"]; console.log("Before Sorting: " + arrayOfNames ); //usage of the function console.log("After Sorting Descending: " + sortArrayOfStrings(arrayOfNames));
Output:-
Before Sorting: George,Veronica,Mounika,Shaunik After Sorting Descending: Veronica,Shaunik,Mounika,George
Explanation:-
Here, the approach is similar. Only we are comparing elements in such a way that we get the order of strings sorted descending.
Read More:
- Javascript: Insert an item to array at specific index
- Javascript: Check if an array is empty
- Javascript: Convert array to string (4 ways)
I hope this article helped you sort an array of strings in javascript. Good Luck !!!