diff --git a/algorithm.go b/algorithm.go new file mode 100644 index 0000000..81a1c33 --- /dev/null +++ b/algorithm.go @@ -0,0 +1,29 @@ +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) +}