q5/algorithm.go
aozhiwei 3eeb9788ce 1
2024-04-09 19:30:17 +08:00

30 lines
514 B
Go

package q5
import (
"sort"
)
type sortImpl[T any] struct {
data []*T
lessCb func(*T, *T) bool
}
func (this *sortImpl[T]) Len() int {
return len(this.data)
}
func (this *sortImpl[T]) Swap(i int, j int) {
this.data[i], this.data[j] = this.data[j], this.data[i]
}
func (this *sortImpl[T]) Less(i int, j int) bool {
return this.lessCb(this.data[i], this.data[j])
}
func Sort[T any](data []*T, lessCb func(*T, *T) bool) {
impl := new(sortImpl[T])
impl.data = data
impl.lessCb = lessCb
sort.Sort(impl)
}