Sort an Array of Strings Alphabetically in Javascript

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 !!!

Advertisements

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top