Maps:to_list/1 element order.

Valentin Micic v@REDACTED
Fri Feb 28 10:44:53 CET 2020


Even though documentation indicates that:

to_list(Map) -> [{Key, Value}]

Map = map()
Key = Value = term()

Returns a list of pairs representing the key-value associations of Map, where the pairs [{K1,V1}, ..., {Kn,Vn}] are returned in arbitrary order.

It appears that the list returned has been sorted in ascending order, using value of the key as a criterion for sorting.
I did some testing using a bit modified example offered in documentation:

(x@REDACTED)47> f(Map), Map = #{42 => value_three,1337 => "value two","a" => 1, 1 => "Last entered"},maps:to_list(Map).  
[{1,"Last entered"},
 {1337,"value two"},

When I changed 1337 to -1337, I get the result that indicates that the list of pairs is *not* returned in arbitrary order, but actually sorted:

(x@REDACTED)48> f(Map), Map = #{42 => value_three, -1337 => "value two","a" => 1, 1 => "Last entered"},maps:to_list(Map).
[{-1337,"value two"},
 {1,"Last entered"},

Could one relay on this always being the case?
A I need this list to be sorted, I would hate to attempt sorting the already sorted list.

Thanks in advance


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the erlang-questions mailing list