139

I am trying to sort an array by 'name' value (using Lodash). I used the Lodash docs to create the solution below however .orderBy doesn't seem to be having any affect at all. Can anyone shed some light on the correct way to sort array?

Chars Array

[  
   {  
      "id":25,
      "name":"Anakin Skywalker",
      "createdAt":"2017-04-12T12:48:55.000Z",
      "updatedAt":"2017-04-12T12:48:55.000Z"
   },
   {  
      "id":1,
      "name":"Luke Skywalker",
      "createdAt":"2017-04-12T11:25:03.000Z",
      "updatedAt":"2017-04-12T11:25:03.000Z"
   }
]

Function Code

 var chars = this.state.characters;

 _.orderBy(chars, 'name', 'asc'); // Use Lodash to sort array by 'name'

 this.setState({characters: chars})
Damian Pavlica
  • 26,228
  • 8
  • 66
  • 73

3 Answers3

260

This method orderBy does not change the input array, you have to assign the result to your array :

var chars = this.state.characters;

chars = _.orderBy(chars, ['name'],['asc']); // Use Lodash to sort array by 'name'

 this.setState({characters: chars})
Artémis Young
  • 2,882
  • 1
  • 12
  • 17
  • 1
    hmmm, still doesn't seem to sorting array. Not sure if I should update question or make a new one –  Apr 12 '17 at 13:41
  • 1
    Updated the answer, maybe your error is due to passing two parameters instead of an array for `'name', 'asc'` – Artémis Young Apr 12 '17 at 13:43
  • 1
    "`orderBy` does not change the input array" ... "`orderBy` changes the array in place." The intended wording is probably "not-in-place" or "out-of-place". See: https://en.wikipedia.org/wiki/In-place_algorithm – Dem Pilafian Aug 21 '19 at 22:33
  • Actually you're right, I just removed this sentence since it added nothing to the answer – Artémis Young Aug 22 '19 at 11:59
  • That second parameter `['asc']` doesn't order ascending, to achieve that the way would be to add a `reverse()` after the `orderBy` (at least in version 4.17.15) – Diego Ortiz Sep 14 '19 at 22:31
  • The documentation for 4.17.15 still says that the last parameter can be used to set the order : https://lodash.com/docs/4.17.15#orderBy – Artémis Young Sep 17 '19 at 12:54
79

You can use lodash sortBy (https://lodash.com/docs/4.17.4#sortBy).

Your code could be like:

const myArray = [  
   {  
      "id":25,
      "name":"Anakin Skywalker",
      "createdAt":"2017-04-12T12:48:55.000Z",
      "updatedAt":"2017-04-12T12:48:55.000Z"
   },
   {  
      "id":1,
      "name":"Luke Skywalker",
      "createdAt":"2017-04-12T11:25:03.000Z",
      "updatedAt":"2017-04-12T11:25:03.000Z"
   }
]

const myOrderedArray = _.sortBy(myArray, o => o.name)
Caio Santos
  • 1,458
  • 11
  • 9
10

If you need to sort by date, you could use this solution:

orderBy(items, (a) => new Date(a.createdAt), ['asc']) // or 'desc'

If you are using moment library, then:

orderBy(items, (a) => moment(a.createdAt), 'asc')

P.s. Note that the lodash doesn't sort dates in string format, so you must convert it to Date object.

Damian Pavlica
  • 26,228
  • 8
  • 66
  • 73