<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Thanks Ulf.<div class=""><br class=""></div><div class="">Indeed, there’s always a cost. However, one should not inflict more cost than actually necessary, hence my question.</div><div class=""><br class=""></div><div class="">As for my specific “problem”, well, there are easier (and probably more efficient) ways than to convert map to list, thus, having a version of <a href="maps:to_list/2" class="">map:to_list/2</a> with the second argument explicitly specifying, say, a sorting order, would probably have just a *marginal* utility value, even if it would be faster than calling map:to_list/1 followed by lists:sort/1. </div><div class=""><br class=""></div><div class="">V/</div><div class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 28 Feb 2020, at 12:00, Ulf Wiger <<a href="mailto:ulf@wiger.net" class="">ulf@wiger.net</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">I should perhaps clarify that the cost of 'canonical' (in this case,<br class="">sorting) would of course still be lower than calling lists:sort/1 to<br class="">get what you need. Presumably, if you actually need the elements<br class="">ordered, there will always be a cost.<br class=""><br class="">BR,<br class="">Ulf W<br class=""><br class="">Den fre 28 feb. 2020 kl 10:58 skrev Ulf Wiger <<a href="mailto:ulf@wiger.net" class="">ulf@wiger.net</a>>:<br class=""><blockquote type="cite" class=""><br class="">This was discussed at length in a previous thread:<br class=""><br class=""><a href="http://erlang.org/pipermail/erlang-questions/2017-October/093981.html" class="">http://erlang.org/pipermail/erlang-questions/2017-October/093981.html</a><br class=""><br class="">The short answer is "no", you can't rely on the order, and this should<br class="">perhaps be more clearly documented. In the future, it might be nice<br class="">with some 'canonical' option to e.g. term_to_binary/2, to ensure that<br class="">encoding is consistent. This would of course come at a cost.<br class=""><br class="">BR,<br class="">Ulf W<br class=""><br class="">Den fre 28 feb. 2020 kl 10:45 skrev Valentin Micic <v@micic.co.za>:<br class=""><blockquote type="cite" class=""><br class="">Hi<br class=""><br class="">Even though documentation indicates that:<br class=""><br class="">to_list(Map) -> [{Key, Value}]<br class=""><br class="">Types<br class=""><br class="">Map = map()<br class="">Key = Value = term()<br class=""><br class=""><br class="">Returns a list of pairs representing the key-value associations of Map, where the pairs [{K1,V1}, ..., {Kn,Vn}] are returned in arbitrary order.<br class=""><br class="">It appears that the list returned has been sorted in ascending order, using value of the key as a criterion for sorting.<br class="">I did some testing using a bit modified example offered in documentation:<br class=""><br class=""><br class="">(x@MacBook-Pro)47> f(Map), Map = #{42 => value_three,1337 => "value two","a" => 1, 1 => "Last entered"},maps:to_list(Map).<br class="">[{1,"Last entered"},<br class=""> {42,value_three},<br class=""> {1337,"value two"},<br class=""> {"a",1}]<br class=""><br class=""><br class=""><br class="">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:<br class=""><br class=""><br class="">(x@MacBook-Pro)48><br class="">(x@MacBook-Pro)48> f(Map), Map = #{42 => value_three, -1337 => "value two","a" => 1, 1 => "Last entered"},maps:to_list(Map).<br class="">[{-1337,"value two"},<br class=""> {1,"Last entered"},<br class=""> {42,value_three},<br class=""> {"a",1}]<br class=""><br class=""><br class="">Could one relay on this always being the case?<br class="">A I need this list to be sorted, I would hate to attempt sorting the already sorted list.<br class=""><br class="">Thanks in advance<br class=""><br class="">V/<br class=""><br class=""></blockquote></blockquote></div></div></blockquote></div><br class=""></div></body></html>