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})
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…