3 ms·
Can any "idiomatic" Go programmers here give a better example of how you would do this? Sure. sort.Sort(sort.Reverse(sort.StringSlice(vs)));
by colin_mccabe 13y ago
Can any "idiomatic" Go programmers here give a better example of how you would do this?
Sure.
sort.Sort(sort.Reverse(sort.StringSlice(vs)));
- colin_mccabe 13y agoOops, I looked at the Python code again, and it seems to be sorting the strings by their length, not reversing the sort order like I did. Sorry, I didn't look at it very carefully earlier. Anyway, the easiest thing to do is probably write a wrapper type around StringSlice using Go's anonymous struct syntax. You only need to implement LessThan, since StringSlice already implements the other two. type StringSliceByLength struct { sort.StringSlice; } func (s StringSliceByLength) LessThan(i, j int) bool { return len(s.StringSlice[i]) < len(s.StringSlice[j]); } Sort.sort(StringSliceByLength(vs))