How to useArray.sort() with Intl.Collator in Javascript
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:
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' ]
JavaScript Program to Sort Strings in Alphabetical Order Ignoring Case
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']
Contact Us