indent goes well beyond indentations too, despite the name :)
http://linux.die.net/man/1/indent
with this: https://golang.org/cmd/gofmt/
even just by line count, you'll see the difference in philosophy.http://stackoverflow.com/questions/19946992/sorting-a-map-of...
(shortest way to sort an array of a custom datatype in go is around 50 lines of code, and requires you to write a custom sorting class)
Convert the map values to a slice and then use the stdlib package sort to organize the data.
type dataSlice []*data
// Len is part of sort.Interface.
func (d dataSlice) Len() int {
return len(d)
}
// Swap is part of sort.Interface.
func (d dataSlice) Swap(i, j int) {
d[i], d[j] = d[j], d[i]
}
// Less is part of sort.Interface. We use count as the value to sort by
func (d dataSlice) Less(i, j int) bool {
// WRONG : will crash if there's a nil in the list ...
return d[i].count < d[j].count
// Correct version
if d[i] == nil {
return true // note : true is an exported name, with no capital ... yet another inconsistency
}
if d[j] == nil {
return false
}
return d[i].count < d[j].count
}
func main() {
// create s of type []*data
sort.Sort(s)
}
Python version: // create list of data in variable s
s.sort(key=lambda x: x.count)
C++ version: // Create Vector<Data> in s
sort(s.begin(), s.end(), [](const Data& d1, const Data& d2) { return d1.count < d2.count; });
(bonus for the C++ version : it's behavior is defined and correct for nulls, just saying this because none of the other examples are, including the Go one)
Java version: // Create List<Data> in s
Collections.sort(s, (Data d1, Data d2) -> Integer.compare(d1.count, d2.count));
Let's put it this way. If Java is 16 TIMES more concise than your language, you have a problem. A big problem.