如何基于属性组织结构列表

时间:2018-04-29 18:40:59

标签: elixir

如何根据这些结构中的一个数字参数组织这个团队列表?

defmodule Seeder do
    def start() do
        team1 = %Team{name: "Team ", points: "10"}
        team2 = %Team{name: "Team 2", points: "5"}
        team3 = %Team{name: "Team 3", points: "4"}
        team4 = %Team{name: "Team 4", points: "3"}
        team5 = %Team{name: "Team 5", points: "1"}
        team6 = %Team{name: "Team 6", points: "0"}

        result = calculate([team1, team2, team3, team4, team5, team6])
        IO.puts(result)
    end

    def calculate(teams) do
        teams
        |> Enum.map()
    end
end

1 个答案:

答案 0 :(得分:0)

谢谢,@ Dogbert。

defmodule Seeder do
    def start() do
        team1 = %Team{name: "Team 1", points: "1"}
        team2 = %Team{name: "Team 2", points: "5"}
        team3 = %Team{name: "Team 3", points: "10"}
        team4 = %Team{name: "Team 4", points: "3"}
        team5 = %Team{name: "Team 5", points: "1"}
        team6 = %Team{name: "Team 6", points: "0"}

        result = calculate([team1, team2, team3, team4, team5, team6])
        IO.inspect(result)
    end

    def calculate(teams) do
        teams
        |> Enum.sort_by(fn team -> String.to_integer(team.points) end)
    end
end