github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/src/cmd/internal/obj/bootstrap.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build !go1.8 6 7 package obj 8 9 import ( 10 "reflect" 11 "sort" 12 ) 13 14 func SortSlice(slice interface{}, less func(i, j int) bool) { 15 val := reflect.ValueOf(slice) 16 tmp := reflect.New(val.Type().Elem()).Elem() 17 x := sliceByFn{val: val, tmp: tmp, less: less} 18 sort.Sort(x) 19 } 20 21 type sliceByFn struct { 22 val reflect.Value 23 tmp reflect.Value 24 less func(i, j int) bool 25 } 26 27 func (x sliceByFn) Len() int { return x.val.Len() } 28 func (x sliceByFn) Less(i, j int) bool { return x.less(i, j) } 29 func (x sliceByFn) Swap(i, j int) { 30 a, b := x.val.Index(i), x.val.Index(j) 31 x.tmp.Set(a) 32 a.Set(b) 33 b.Set(x.tmp) 34 }