github.com/wfusion/gofusion@v1.1.14/common/utils/sort.go (about) 1 package utils 2 3 import "sort" 4 5 func Sort[E any](data []E, cmp func(e1, e2 E) bool) { 6 sortObj := sortable[E]{data: data, cmp: cmp} 7 sort.Sort(sortObj) 8 } 9 10 func SortStable[E any](data []E, cmp func(e1, e2 E) bool) { 11 sortObj := sortable[E]{data: data, cmp: cmp} 12 sort.Stable(sortObj) 13 } 14 15 type sortable[E any] struct { 16 data []E 17 cmp func(e1, e2 E) bool 18 } 19 20 func (s sortable[E]) Len() int { return len(s.data) } 21 func (s sortable[E]) Swap(i, j int) { s.data[i], s.data[j] = s.data[j], s.data[i] } 22 func (s sortable[E]) Less(i, j int) bool { return s.cmp(s.data[i], s.data[j]) }