github.com/mymmsc/gox@v1.3.33/util/sort.go (about) 1 // Copyright (c) 2015, Emir Pasic. 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 package util 6 7 import "sort" 8 9 // Sort sorts values (in-place) with respect to the given comparator. 10 // 11 // Uses Go's sort (hybrid of quicksort for large and then insertion sort for smaller slices). 12 func Sort(values []interface{}, comparator Comparator) { 13 sort.Sort(sortable{values, comparator}) 14 } 15 16 type sortable struct { 17 values []interface{} 18 comparator Comparator 19 } 20 21 func (s sortable) Len() int { 22 return len(s.values) 23 } 24 func (s sortable) Swap(i, j int) { 25 s.values[i], s.values[j] = s.values[j], s.values[i] 26 } 27 func (s sortable) Less(i, j int) bool { 28 return s.comparator(s.values[i], s.values[j]) < 0 29 }