Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 27 lines (23 sloc) 0.465 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
package sort_with
import (
  "sort"
  "fmt"
)

type sortWrap struct {
  sl []interface{}
  fn func(interface{}, interface{}) bool
}

func (sw sortWrap) Len() int {
  return len(sw.sl)
}
func (sw sortWrap) Swap(i, j int) {
  s := sw.sl
  s[i], s[j] = s[j], s[i]
}
func (sw sortWrap) Less(i, j int) bool {
  return sw.fn(sw.sl[i], sw.sl[j])
}

type LessFn func(interface{}, interface{}) bool

func SortWith(v []interface{}, fn LessFn) {
  sort.Sort(sortWrap{v, fn})
}
Something went wrong with that request. Please try again.