sort.go 467 B

1234567891011121314151617181920212223242526272829
  1. package gods
  2. type Getter[V any] interface {
  3. Get(int) V
  4. }
  5. type Swapper interface {
  6. Swap(i, j int)
  7. Len() int
  8. }
  9. type CustomSorter[V any] interface {
  10. Getter[V]
  11. Swapper
  12. }
  13. type LessFunc[V any] func(v1, v2 V) bool
  14. // The type implements way to sort
  15. // swappers via custom function.
  16. type CustomSort[V any] struct {
  17. CustomSorter[V]
  18. LessFunc LessFunc[V]
  19. }
  20. func (cs CustomSort[V]) Less(i, j int) bool {
  21. vi, vj := cs.Get(i), cs.Get(j)
  22. return cs.LessFunc(vi, vj)
  23. }