JavaScript Program to Sort Strings in Alphabetical Order Ignoring Case
Last Updated :
27 May, 2024
In this article, we are given an array of strings, you need to sort the given array of strings containing both uppercase and lowercase characters in ascending order by ignoring the case in JavaScript.
Example 1:
Input :-
arr = ["Geeks", "for", "geeks", "is", "The", "Best"]
Output :-
[ 'Best', 'for', 'Geeks', 'geeks', 'is', 'The' ]
Explanation:
In the input array after looking at starting character of each string we find that :
B < F < G < I <T.
So "Best" will come first followed by "For" then "Geeks" two times then "Is" and finally "The"
Example 2:
Input :-
arr = ["Hey", "How", "You", "Doing" ]
Output :-
['Doing', 'Hey', 'How', 'You']
Approaches to sort strings in alphabetical order ignoring case in JavaScript
Approach 1: Using a comparator function
In this approach, we Initialize the array and use the sort method to sort the array in alphabetical order ignoring case. The sort method then takes a comparator function as its argument. The comparator function compares two elements of the array a and b and then it returns a negative number if a comes before b, a positive number if a comes after b, or 0 if both a and b are equal.
Example: Below is the implementation of this approach
JavaScript
let arr = ["Geeks", "for", "geeks", "is","The","Best"];
arr.sort(function(a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
console.log(arr);
Output[ 'Best', 'for', 'Geeks', 'geeks', 'is', 'The' ]
Approach 2: Without using the comparator function
Convert the strings to lowercase by using the toLowerCase method, and then compare them using the ternary operator ? . If the lowercase version of string a comes after the lowercase version of string b, then return 1 which means that a should come after b in the sorted array. Otherwise, return -1 meaning that a should come before b in the sorted array.
Example: Below is the implementation of this approach
JavaScript
let arr = ["Geeks", "for", "geeks", "is","The","Best"];
arr.sort((a, b) => a.toLowerCase() > b.toLowerCase() ? 1 : -1);
console.log(arr);
Output[ 'Best', 'for', 'geeks', 'Geeks', 'is', 'The' ]
Approach 3: Using Array.sort() with Intl.Collator
Using Array.sort() with Intl.Collator, initialize Collator with { sensitivity: 'accent' } to perform a case-insensitive comparison. Then, sort the array of strings, considering language-specific rules for sorting while ignoring case differences.
Example:
JavaScript
const strings = ["Apple", "banana", "Orange", "grape"];
const collator = new Intl.Collator(undefined, { sensitivity: 'accent' });
strings.sort(collator.compare);
console.log(strings);
Output[ 'Apple', 'banana', 'grape', 'Orange' ]
Similar Reads
JavaScript Program to Sort Words in Alphabetical Order Sorting words in alphabetical order involves arranging them based on the standard sequence of letters in the alphabet. This process helps in organizing data, making searches efficient, and presenting information in a clear and systematic manner.Methods to sort wordsTable of Content Approach 1: Using
5 min read
Java Program to Sort Names in an Alphabetical Order For, sorting names in an Alphabetical order there are multiple ways to sort the array, like using inbuilt Arrays.sort() method or using normal sorting algorithms like the bubble sort, merge sort. Here let's use the bubble sort and inbuilt sort. Example: Input : Array[] = {"Sourabh", "Anoop, "Harsh",
3 min read
PHP Program to Sort Names in an Alphabetical Order Sorting names in alphabetical order is a common task in programming. PHP provides several functions and methods to achieve this. Examples: Input: arr = ["Sourabh", "Anoop, "Harsh", "Alok", "Tanuj"]Output: ["Alok", "Anoop", "Harsh", "Sourabh", "Tanuj"]Input: arr = ["John", "Alice", "Bob", "Eve", "Cha
2 min read
How To Sort List Of Strings In Alphabetically When working with Python programs, be it any machine learning program or simple text processing program, you might have come across a need to sort a list of strings alphabetically, either in ascending or reverse order. Strings are sorted alphabetically based on their initial letter (a-z or A-Z). But
3 min read
JavaScript - Sort a String Alphabetically using a Function Here are the various methods to sort a string alphabetically using a function in JavaScript.1. Using split(), sort(), and join() MethodsThis is the most basic and commonly used method to sort a string alphabetically. The string is first converted into an array of characters, sorted, and then joined
2 min read
Java Program to Sort an Array of Strings in Lexicographical Order (Dictionary Order) In Java, sorting an array in lexicographical (dictionary) order means elements will be arranged based on their alphabetical order from A to Z with case sensitivity.Example:We will use Arrays.sort() method with String.CASE_INSENSITIVE_ORDER to sort an array in lexicographical order. It is the most di
3 min read
Java Program to Sort the Elements of an Array in Ascending Order Here, we will sort the array in ascending order to arrange elements from smallest to largest, i.e., ascending order. So the easy solution is that we can use the Array.sort method. We can also sort the array using Bubble sort.1. Using Arrays.sort() MethodIn this example, we will use the Arrays.sort()
2 min read
How to Sort a String Characters Alphabetically in Ruby? This article focuses on discussing how to sort a string's characters alphabetically in Ruby. Using Chars and SortSyntax: sorted_string = string.chars.sort.join Example: In this example, the string is converted into an array of characters, and the sort method is used to sort the array. Ruby # Origina
2 min read
Python Program to Sort Words in Alphabetical Order The task is to write a program that takes a list of words as input and sorts them in alphabetical order. The program should display the sorted list of words, ensuring that the sorting is case-insensitive. To sort words in alphabetical order in Python, we can use the sorted() function or the sort() m
2 min read
C# Program to Sort Student Names in Descending Order using LINQ Given a list of student names, now our task is to sort the names given in the list in descending order using the LINQ, This task can be done using OrderByDescending() method of LINQ. This method is used to sort the elements in the collection in descending order. While using this method in the LINQ q
2 min read