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

Bit*_*ise 3 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]}]
Run Code Online (Sandbox Code Playgroud)

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

目前的尝试:

Enum.map(points, fn(p) -> p.points end) |> Enum.sort
Run Code Online (Sandbox Code Playgroud)

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

Jos*_*lim 12

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

Enum.sort_by(points, fn(p) -> p.points end)
Run Code Online (Sandbox Code Playgroud)

  • @Everett我添加了更多示例。 (2认同)