Reputation: 311
let arr =
[{
"goodreadsId": "1531073",
"books": {
"title": "pensée antique",
"ratings_count": "1"
}
},
{
"goodreadsId": "1377520",
"books": {
"title": "Why",
"ratings_count": "2"
}
}]
I did _.sortBy(arr, 'books.ratings_count')
I expect the ratings count of 2 will be sorted at the first but it didn't. I also tried _.sortBy(arr, 'books.ratings_count', 'desc')
but it doesn't work, any clue?
Upvotes: 0
Views: 567
Reputation: 5574
let arr =
[{
"goodreadsId": "1531073",
"books": {
"title": "pensée antique",
"ratings_count": "1"
}
},
{
"goodreadsId": "1377520",
"books": {
"title": "Why",
"ratings_count": "2"
}
}]
const sorted = _.sortBy(arr, el => -el.books.ratings_count);
console.log(sorted);
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
Upvotes: 2
Reputation: 10194
It seems you are going to sort array in DESC
order by ratings_count
.
In this case, you should use _.orderBy
function. (_.sortBy
function supports sorting on ASCENDING
order).
let arr = [{
"goodreadsId": "1531073",
"books": {
"title": "pensée antique",
"ratings_count": "1"
}
},
{
"goodreadsId": "1377520",
"books": {
"title": "Why",
"ratings_count": "2"
}
}
];
console.log(_.orderBy(arr, "books.ratings_count", ['desc']));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
Upvotes: 2