I have this list:
[%Statcasters.PredictionScore{__meta__: #Ecto.Schema.Metadata<:loaded, "prediction_scores">,
id: 1, inserted_at: ~N[2018-01-15 20:24:33.838946],
league: #Ecto.Association.NotLoaded<association :league is not loaded>,
league_id: 1, points: 4,
prediction: #Ecto.Association.NotLoaded<association :prediction is not loaded>,
prediction_id: 1,
question: #Ecto.Association.NotLoaded<association :question is not loaded>,
question_id: 1, updated_at: ~N[2018-01-15 20:24:33.838952]},
%Statcasters.PredictionScore{__meta__: #Ecto.Schema.Metadata<:loaded, "prediction_scores">,
id: 2, inserted_at: ~N[2018-01-15 20:24:33.842205],
league: #Ecto.Association.NotLoaded<association :league is not loaded>,
league_id: 1, points: 3,
prediction: #Ecto.Association.NotLoaded<association :prediction is not loaded>,
prediction_id: 2,
question: #Ecto.Association.NotLoaded<association :question is not loaded>,
question_id: 1, updated_at: ~N[2018-01-15 20:24:33.842210]}]
As you can see each map has a points key. I want to sort the maps within the list and return the lowest point integer map to be the first element of the list.
Current attempt:
Enum.map(points, fn(p) -> p.points end) |> Enum.sort
This doesn't work because it returns only the points sorted. I need the entire map.
Solution: The idea is to store the entry set in a list and sort the list on the basis of values. Then fetch values and keys from the list and put them in a new hashmap. Thus, a new hashmap is sorted according to values.
If we need to sort the HashMap by values, we should create a Comparator. It compares two elements based on the values. After that get the Set of elements from the Map and convert Set into the List. Use the Collections.
Step 1: Create a TreeMap in java with a custom comparator. Step 2: Comparator should compare based on values and then based on the keys. Step 3: Put all key-value pairs from the hashmap into the treemap. Step 4: return the treemap.
There are several ways to sort a Map<key, value>. Here we use the sort (), sorted () method and comparator interface, etc. Let’s see the examples. We can use the sort () method of the List interface to sort the elements of Map.
In this example, we use the compareTo () method to compare values of Map<key, value> inside the sort () method as an argument. You can see that we created an anonymous inner class of the Comparator interface and defined the compare () method to compare the values.
Inside the toMap () method, we use the LinkedHashMap::new method reference to retain the sorted order of the map. import static java.util.stream.Collectors.*;
Solution: The idea is to store the entry set in a list and sort the list on the basis of values. Then fetch values and keys from the list and put them in a new hashmap. Thus, a new hashmap is sorted according to values.
I believe Enum.sort_by/3
is what you want:
Enum.sort_by(points, fn(p) -> p.points end)
EDIT: to sort by multiple columns, put them in a tuple/list:
Enum.sort_by(points, fn(p) -> {p.points, p.coordinate} end)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With