Skip to content

Instantly share code, notes, and snippets.

@crhntr
Created January 10, 2019 02:56
Show Gist options
  • Save crhntr/59dbc7bdb72b7469a050e5d5986202da to your computer and use it in GitHub Desktop.
Save crhntr/59dbc7bdb72b7469a050e5d5986202da to your computer and use it in GitHub Desktop.
// Sort "generic"
// https://medium.com/capital-one-tech/closures-are-the-generics-for-go-cb32021fb5b5
type sorter struct {
len int
swap func(i, j int)
less func(i, j int) bool
}
func (x sorter) Len() int { return x.len }
func (x sorter) Swap(i, j int) { x.swap(i, j) }
func (x sorter) Less(i, j int) bool { return x.less(i, j) }
func sortThis(n int, swap func(i, j int), less func(i, j int) bool) { sort.Sort(sorter{n, swap, less}) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment