gpt4 book ai didi

sorting - 根据 map 中的值对 map 列表进行排序

转载 作者:行者123 更新时间:2023-12-04 13:21:04 25 4
gpt4 key购买 nike

我有这个 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]}]

如您所见,每个 map 都有一个点键。我想对列表内的 map 进行排序,并返回最低点整数 map 作为列表的第一个元素。

当前尝试:
Enum.map(points, fn(p) -> p.points end) |> Enum.sort

这是行不通的,因为它仅返回排序的点。我需要整个 map 。

最佳答案

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

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

编辑:按多列排序,将它们放在元组/列表中:
Enum.sort_by(points, fn(p) -> {p.points, p.coordinate} end)

关于sorting - 根据 map 中的值对 map 列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48310861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com