![]() |
VOOZH | about |
When working with data, sorting is important to make it easy to understand. But with words, uppercase and lowercase letters can cause problems. Luckily, Lodash's orderBy function helps sort words without worrying about the case.
There are two approaches to performing case-insensitive sorting using Lodash's orderBy function:
In this approach, we'll create a custom iteratee function that converts the string values to lowercase or uppercase before sorting. This allows us to ignore case sensitivity during the sorting process.
Example: In this example, we use Lodash's orderBy function to sort an array of objects based on the name property. To ensure the sorting is case insensitive, we provide a custom function that converts each name to lowercase before sorting.
Output:
[
{ name: 'ajay' },
{ name: 'geek' },
{ name: 'Pawan' },
{ name: 'SHRINIWAS' }
]
In this approach, we'll use the toLower() or toUpper() function from Lodash to convert the string values to lowercase or uppercase before sorting.
Example: In this example, we use Lodash's orderBy function to sort a list of names without worrying about uppercase or lowercase letters. The _.toLower() function converts all names to lowercase before sorting, making sure they are sorted correctly in alphabetical order.
Output:
[
{ name: 'ajay' },
{ name: 'geek' },
{ name: 'Pawan' },
{ name: 'SHRINIWAS' }
]
In short, Lodash's orderBy function makes it easy to sort a list of words without worrying about uppercase and lowercase letters. We can use a special helper function or a trick to make all the words lowercase or uppercase, so our sorting is more accurate and works as expected.