当前位置:  开发笔记 > 编程语言 > 正文

在Elixir中排序日期列表

如何解决《在Elixir中排序日期列表》经验,为你挑选了1个好方法。

我整理了一个小脚本来帮助我完成伤病恢复过程.它输出了我的预期,但是日期看起来并没有像我期望的那样排序 - 看起来按月/日排序是有效的,但不是按年.如果我只排序日期列表而不使用结构,则结果相同.

附注,使用https://github.com/lau/calendar填充我的日期范围.

defmodule Phase do
  defstruct day: "", weight: "" 
end

defmodule Recovery do
  alias Calendar.Date

  def phases(start_date, end_date, start_weight, increase_by) do
    # Get the range of dates
    date_range = Date.days_after_until(start_date, end_date, true) |> Enum.to_list

    # build all phases, starting at day 0
    values = do_phases(date_range, [], start_weight, increase_by)
    Enum.sort_by(values, &(&1.day))
  end

  defp do_phases([], recovery_plan, _weight, _increase_by), do: recovery_plan 
  defp do_phases([head | tail], recovery_plan, weight, increase_by) do
    v = [%Phase{day: head, weight: weight} | recovery_plan]
    do_phases(tail, v, weight + increase_by, increase_by)
  end
end

在iex中运行输出以下内容:

iex(18)> d1 = Date.from_erl! {2016, 12, 29}
iex(19)> d2 = Date.from_erl! {2017, 1, 19}
iex(24)> Recovery.phases(d1, d2, 30, 5)
[%Phase{day: ~D[2017-01-01], weight: 45},
 %Phase{day: ~D[2017-01-02], weight: 50},
 %Phase{day: ~D[2017-01-03], weight: 55},
 %Phase{day: ~D[2017-01-04], weight: 60},
 %Phase{day: ~D[2017-01-05], weight: 65},
 %Phase{day: ~D[2017-01-06], weight: 70},
 %Phase{day: ~D[2017-01-07], weight: 75},
 %Phase{day: ~D[2017-01-08], weight: 80},
 %Phase{day: ~D[2017-01-09], weight: 85},
 %Phase{day: ~D[2017-01-10], weight: 90},
 %Phase{day: ~D[2017-01-11], weight: 95},
 %Phase{day: ~D[2017-01-12], weight: 100},
 %Phase{day: ~D[2017-01-13], weight: 105},
 %Phase{day: ~D[2017-01-14], weight: 110},
 %Phase{day: ~D[2017-01-15], weight: 115},
 %Phase{day: ~D[2017-01-16], weight: 120},
 %Phase{day: ~D[2017-01-17], weight: 125},
 %Phase{day: ~D[2017-01-18], weight: 130},
 %Phase{day: ~D[2017-01-19], weight: 135},
 %Phase{day: ~D[2016-12-29], weight: 30},
 %Phase{day: ~D[2016-12-30], weight: 35},
 %Phase{day: ~D[2016-12-31], weight: 40}]

nietaki.. 14

看起来你正在使用的库中的日期结构如下所示:%Date{day: 25, month: 12, year: 2014}.在Elixir中,结构按其内容进行比较,按字段的字母顺序排列.在这种情况下,最显著将是day,然后monthyear-你要查找的内容相反.

你可以尝试做的是改变Enum.sort_by(values, &(&1.day))Enum.sort_by(values, &(Date.to_erl(&1.day))).这会将%Date{}结构转换为Erlang元组,它应按预期排序.



1> nietaki..:

看起来你正在使用的库中的日期结构如下所示:%Date{day: 25, month: 12, year: 2014}.在Elixir中,结构按其内容进行比较,按字段的字母顺序排列.在这种情况下,最显著将是day,然后monthyear-你要查找的内容相反.

你可以尝试做的是改变Enum.sort_by(values, &(&1.day))Enum.sort_by(values, &(Date.to_erl(&1.day))).这会将%Date{}结构转换为Erlang元组,它应按预期排序.

推荐阅读
k78283381
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有