Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort List of Maps based on a value within the Map

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.

like image 347
Bitwise Avatar asked Jan 17 '18 22:01

Bitwise


People also ask

Can you sort a map based on value?

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.

How are you to sort given HashMap on basis of 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.

How do you sort a map by key value?

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.

How to sort a map in Java?

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.

How to compare values of map<key> inside the sort () method?

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.

How to retain the sorted order of the map inside tomap?

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.*;

How do you sort a hashmap based on values?

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.


Video Answer


1 Answers

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)
like image 51
José Valim Avatar answered Nov 14 '22 17:11

José Valim