How to perform case-insensitive sorting array of string in JavaScript?
Effectively sort strings in an array without being susceptible to case dependency using the built-in sort()
method and a comparator that leverages toLowerCase()
:
Method Breakdown
There's more than one way to skin a cat, or, in our case, sort strings. Let's dive in!
localeCompare()
in sorting
localeCompare()
provides an option for easy case-insensitive sorting:
In the above example, { sensitivity: 'base' }
directs localeCompare
to ignore differences in case and accents -- which is what we want for universal case-insensitive sorting.
Efficient sorting with Intl.Collator()
If you need to optimize performance for large datasets or frequent sort operations, Intl.Collator().compare
is your pal:
Sorting Pitfalls
When sorting strings, there are few things to keep in mind.
Execution order
Ensuring .toLowerCase().localeCompare()
sequence is critical for uniformity before comparison.
More than just case-insensitivity
Create a more sophisticated sorting logic including numbers and special characters when your string sorting goes beyond simple cases:
Differences in sort method behaviour
The Array.sort()
method treats each item as a Unicode point, not as a string, causing unexpected results.
Custom Sorting for Accuracy
Accuracy, as they say, takes you from good to great.
Handling corner cases
Testing your sorting method against inputs like numeric strings, accented characters and multiple languages ensures a reliable output.
Cross-browser compatibility
Cross-check the compatibility of localeCompare()
across different browsers to ensure consistent results.
Looks like a sorted job!
The principle here is simple - the sorting mechanism treats uppercase and lowercase as equal, letting you focus on what really matters.`
Best Practices for Readability
Refactor your code for legibility and extendibility.
The DRY principle
Develop a reusable sortCaseInsensitive()
function:
Better refactor for toLowerCase()
Assign toLowerCase()
to variables before comparison to evade duplicate calls:
Standardize similar strings
Normalize the strings to improve their comparability using normalize()
:
Was this article helpful?