根据地图中的值对地图列表进行排序

时间:2018-01-17 22:11:53

标签: sorting elixir phoenix-framework

我有这个清单:

  [%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]}]

正如您所看到的,每张地图都有一个点键。我想对列表中的地图进行排序,并将最低点整数映射返回到列表的第一个元素。

目前的尝试:

Enum.map(points, fn(p) -> p.points end) |> Enum.sort

这不起作用,因为它只返回已排序的点。我需要整张地图。

1 个答案:

答案 0 :(得分:10)

我相信Enum.sort_by/2就是你想要的:

Enum.sort_by(points, fn(p) -> p.points end)