

Sort an Array of Strings Alphabetically in Javascript
source link: https://thispointer.com/sort-an-array-of-strings-alphabetically-in-javascript/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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:-
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:
I hope this article helped you sort an array of strings in javascript. Good Luck !!!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK