3 ms·
I'm curious about the specifics of your problem. I couldn't imagine sorting a list of structs (by one of the fields I presume) would be too terribly different i
by okbake 12y ago
I'm curious about the specifics of your problem. I couldn't imagine sorting a list of structs (by one of the fields I presume) would be too terribly different in Go than in other langauges. Heres an example of insertion sort on a basic type: https://play.golang.org/p/SPoiNRVl2B https://play.golang.org/p/SPoiNRVl2B
You can use the standard library sort methods by making your type implement the sort interface. Heres an example taken from the example in the sort docs: https://play.golang.org/p/oeRIhHi1Ei https://play.golang.org/p/oeRIhHi1Ei
- waps 12y agoThis post contains a comparison : https://news.ycombinator.com/item?id=9267578 https://news.ycombinator.com/item?id=9267578
- he_the_great 12y agoI see you've grabbed some examples to show the simplicity. Having to write the insertion sort algorithm to sort your basic types is not simple. Having to tell the standard library how to swap data in a slice and to obtain a slices length is simple... why doesn't the standard library know how to work with basic types? This way I don't end up writing by accident: func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[j] }