New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: Add a sorting function for map #39291
Comments
Maps in go are not sorted, even if it was possible to sort a maps keys, it wouldn’t store them, and iteration order over a map is not defined. |
If Go had generics (#15292), one could imagine something like: func SortedKeys(type K ordered, V)(m map[K]V) []K {
keys := make([]K)
for k := range m {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
return keys
} Obviously, this will incur Barring generics, I don't think anything like this should be considered yet. |
You can see something about how hard this problem is by looking at the implementation that the fmt package uses to provide a stable sorted output when printing a map. |
The code for fmt stable sorting of a map for reference: https://github.com/golang/go/blob/master/src/internal/fmtsort/sort.go |
I'm not sure what this proposal is asking for. Do you want a way to keep a map in sorted order? That would be a significant language change and is very unlikely to be accepted. Or do you want a way to take a map and return the elements sorted by key? That is easy to write for any specific map and easy to write if we have generics. In order to evaluate this proposal, you need to suggest an API. Thanks. |
I'm sorry, but you seem to misunderstand what I mean.I'm not asking the map is already sorted.I want to add a function is used to sort the map. |
Thank you for explaining. Where are you going to store the output of sorting the map? If its another variable then this is already possible https://play.golang.org/p/wHZ4LnEvhx_6 If you want the map to become sorted, that is the order of keys in the map is sorted, this is not possible. |
You want to add sorted map? This goes against golang design. In fact, golang randomizes order of map on every run. Taking into account that golang does not have this container for 10 years and just general philosophy of go is to keep core code minimal, I don't think this idea will get accepted. You have better chances submitting proposal to this repo that contains enough stars and containers already. |
okay, Thanks for your explanation, I get it. |
It seems that this proposal will not be accepted, thank you for your reply. |
Oh well, I understand what you mean, thank you for your reply. |
hello
Usually, I often need to sort the map conveniently, sorting by key or value. I don't know if this function can be added, or for what reason not to increase this function? If you want, I can add this function.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
The text was updated successfully, but these errors were encountered: