How to make a map order by date

Hello everyone, today I have a problem with Map Module.
I want to new a map like this

"2018-04-13" => 101, 
"2018-04-12" => 34
"2018-03-13" => 345

key is date and value is random number, and order is by date.But when I merge %{“2018-03-12” => 134}, the order is upset. It becomes

"2018-03-17" => xxx,
"2018-04-01" =>xxx,

And I found that if last date compared first date more than a month, the order will be upset.So please tell me why?Thank you very much!

Keys in maps should be considered not ordered.

If you want an ordered collection, try using a list of {key, value} tuples.

data = [
  {"2018-04-13", 101},
  {"2018-04-12", 34}

Also, for faster sorting, you might want to represent dates as unix timestamps …


Thanks a lot! I found this
「Maps’ keys do not follow any ordering.」
in elixir get started maps page.

1 Like

Anyone stumbling upon this:
Enum.sort can help. Then you have a Keylist. They are ordered.

1 Like