I'm trying to sort an array of objects by the average property in descending order - so the largest average is first - but am not able to using underscore.js. Below is my attempt:
var jsonData = [
{
"title": "Dear Kitten",
"totalCount": 1689,
"average": 241
},
{
"title": "Weird Things All Couples Fight About",
"totalCount": 9966,
"average": 1424
},
{
"title": "If Disney Princesses Were Real",
"totalCount": 16567,
"average": 2367
},
{
"title": "Secret Tricks With Everyday Objects",
"totalCount": 24884,
"average": 3555
},
{
"title": "The Coolest Travel Hacks",
"totalCount": 41847,
"average": 8369
},
{
"title": "5 Ways You're Drinking Coffee Wrong",
"totalCount": 55673,
"average": 7953
},
{
"title": "The Perfect Way To Pour A Beer",
"totalCount": 58097,
"average": 58097
},
{
"title": "Fruit You're Eating Wrong",
"totalCount": 65570,
"average": 32785
},
{
"title": "Your Cat Is Judging You",
"totalCount": 78952,
"average": 11279
},
{
"title": "3rd Date vs 30th Date",
"totalCount": 84394,
"average": 14066
}
];
console.log(_.sortBy(jsonData, "average"));
averagefirst? Use.reverse()after the sort, or provide your owniterateethat reverses it, e.g.function( item ) { return -item.average; }.