You left out defining a sorting class, like in Turbo Pascal. So, it's 17 lines of code (wrong version provided on stackoverflow) or 21 lines, the correct version :
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.