119

I checked this answer but to achieve the same result, that is to get case-insensitive sorting, I need to use orderBy instead of sortBy since it gives the ability to specify the sort order.

The only way I found to achieve it was to create a cloned "middle" array mapped to lower case the name :

const users = [
  { name: 'A', age: 48 },
  { name: 'B', age: 34 },
  { name: 'b', age: 40 },
  { name: 'a', age: 36 }
];

let lowerCaseUsers = _.clone(users);

lowerCaseUsers = lowerCaseUsers.map((user) => {
  user.name = user.name.toLowerCase();
  return user;
});

const sortedUsers = _.orderBy(lowerCaseUsers, ['name'], ['desc']);

console.log(sortedUsers);

This seems really expensive and it will even be more complex with multiple sortings and dynamic properties names.

Is there a better idea ?


Here is a Plunker : https://plnkr.co/edit/i1ywyxjFctuNfHtPTcgG

Community
  • 1
  • 1
WhiteEleven
  • 1,193
  • 2
  • 7
  • 6

3 Answers3

244

The documentation specifies that you can pass a function as "iteratee":

[iteratees=[_.identity]] (Array[]|Function[]|Object[]|string[]): The iteratees to sort by.

So you can do

const users = [
  { name: 'A', age: 48 },
  { name: 'B', age: 34 },
  { name: 'b', age: 40 },
  { name: 'a', age: 36 }
];

const sortedUsers = _.orderBy(users, [user => user.name.toLowerCase()], ['desc']);
console.log(sortedUsers);
<script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js"></script>
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 2
    This may fail on older browsers. – steampowered Jul 20 '17 at 17:30
  • 23
    If you mean that this will throw a syntax error in browser not supporting ES6, then yes, of course. I'll leave converting the arrow function to a "normal" function (and `const` to `var`) as exercise to the reader. – Felix Kling Jul 20 '17 at 17:32
  • @TheCaptan: there is no reason why it should not. – Felix Kling Sep 22 '17 at 15:20
  • 3
    I'm attempting @TheCaptan's question with the following, but get a compiler error: `const sortedUsers = _.orderBy(users, [user => user[propToSort].toLowerCase()], ['desc']);` – mellis481 Nov 16 '17 at 21:14
  • 2
    @im1dermike: you probably want to use `.toString()` before calling to lowercase there, additionally if you want to use `property.something` for propToSort and similar you can use `_.get(user, propToSort)` (maybe this will help someone else) – Soniku Jul 30 '18 at 17:29
22

Ordering by multiple properties:

const users = [
  { name: 'A', age: 48 },
  { name: 'B', age: 34 },
  { name: 'b', age: 40 },
  { name: 'a', age: 36 }
]

const nameSorter = user => user.name.toLowerCase()
const ageSorter = 'age'

const sortedUsers = _.orderBy(users, [nameSorter, ageSorter], ['desc', 'asc'])
Be Kind
  • 4,712
  • 1
  • 38
  • 45
MaxineHu
  • 349
  • 2
  • 4
5

You can combine Felix Kling example with _.get function to sort by dynamic nested attributes:

const _ = require('lodash');

let test = [{ a: {b:'AA'}},{a: {b:'BB'} }, {a: {b: 'bb'}}, {a:{b:'aa'}}];

let attrPath = 'a.b';

_.orderBy(test, [item => _.get(item, attrPath).toLowerCase()]);
Agustí Sánchez
  • 10,455
  • 2
  • 34
  • 25