Closed Alexius-Huang closed 7 years ago
@Maxwell-Alexius This is because we use Go's map to store pairs, and in Go map's key/value are random ordered.
I'll try to find a solution for this case, thanks!
@st0012 Maybe we can sort the keys in alphabetical order so that we can guarantee that the hash will be appear in certain order in Goby lang?
https://stackoverflow.com/questions/23330781/sort-golang-map-values-by-keys
Sort keys in alphabetical order: https://stackoverflow.com/questions/2038508/easy-way-to-get-the-keys-in-a-map-in-alphabetical-order
@Maxwell-Alexius I think we should just sort them in toString
method, and keep it random in other places. I remember I read a post about why Go made this random decision but I can't find it now. @hachi8833 Do you know why?
Looks like the article is the one.
@st0012 @hachi8833 Thanks, I think I can adjust this feature request which is listed below:
Proposal I. There is no need to sort or order the Hash key-value pairs. However, for some method implementation, like Hash#sorted_keys
, we can return an array of keys of hash in alphabetical order. (Affect some Hash API method implementation in #247)
~Proposal II. Remain current arbitrary state of the key-value pairs in hash API, so if implementing Hash#keys
, the keys may also in an arbitrary order. (Issue closed here, if anyone wants to change the Hash pairs order, they can reopen this issue)~
I actually agree that the hash shouldn't guarantee the key's order, so I suggest keep it this way. But maybe we can provide some extra API like sorted_keys
for those who need the keys sorted.
Okay, got it ~
This should be closed because PR #247 completed.
The hash pair order in REPL seems not corresponding to the input order, e.g., when I try to print out simple hash:
Any idea?