github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/base/bcomparator/sort.go (about) 1 package bcomparator 2 3 import "sort" 4 5 func Sort[E any](values []E, comparator Comparator[E]) { 6 sort.Sort(sortable[E]{values, comparator}) 7 } 8 9 type sortable[E any] struct { 10 values []E 11 comparator Comparator[E] 12 } 13 14 func (s sortable[E]) Len() int { 15 return len(s.values) 16 } 17 func (s sortable[E]) Swap(i, j int) { 18 s.values[i], s.values[j] = s.values[j], s.values[i] 19 } 20 func (s sortable[E]) Less(i, j int) bool { 21 return s.comparator(s.values[i], s.values[j]) < 0 22 }