In Elixir, I have a list, which is probably this:
[
%{id:1, Retweet_count:10, favorite_count:20},
%{id:2, Retweet_count:20, favorite_count:1},
%{id:3, Retweet_count:15, favorite_count:50},
]
Now you want to sort by the total number of retweet_count and favorite_count in descending order.
In JavaScript, it's very simple:
List.sort (function (A, b) {
if (A.retweet_count + a.favorite_count > b.retweet_count + b.favorite_count) {
Return-1
else if (A.retweet_count + a.favorite_count < B.retweet_count + B.favorite_count) {
Return 1
}
return 0
})
Elixir has a similar approach to sort_by:
Sort (enumerable, fun)
So our descending order rules are written as follows:
enum.sort [
%{id:1, Retweet_count:10, favorite_count:20},
%{id : 2, Retweet_count:20, favorite_count:1},
%{id:3, retweet_count:15, favorite_count:50},
&n Bsp ], & (Map.get (&1,: retweet_count) + Map.get (&1,: Favorite_count) >= map.get (&2,: retweet_count) + Map.get (&2,: favorite_count))