github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/slice/clone/stablesort/api.go (about)

     1  // Package stablesort provides stable sorting of cloned slice elements
     2  package stablesort
     3  
     4  import (
     5  	"golang.org/x/exp/constraints"
     6  
     7  	"github.com/m4gshm/gollections/slice"
     8  	"github.com/m4gshm/gollections/slice/clone"
     9  )
    10  
    11  // By makes clone of stable sorted elements by converting them to Ordered values and applying the operator <
    12  func By[T any, O constraints.Ordered, TS ~[]T](elements TS, orderConverter func(T) O) TS {
    13  	return slice.StableSortAsc(clone.Of(elements), orderConverter)
    14  }
    15  
    16  // ByLess makes clone and atanle sorts elements using a function that checks if an element is smaller than the others
    17  func ByLess[T any, TS ~[]T](elements TS, comparer func(T, T) int) TS {
    18  	return slice.StableSort(clone.Of(elements), comparer)
    19  }
    20  
    21  // Asc makes clone of stable sorted orderable elements
    22  func Asc[T constraints.Ordered, TS ~[]T](elements TS) TS {
    23  	return By(elements, func(o T) T { return o })
    24  }