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

     1  // Package sort provides sorting of cloned slice elements
     2  package sort
     3  
     4  import (
     5  	"golang.org/x/exp/constraints"
     6  
     7  	"github.com/m4gshm/gollections/slice/clone"
     8  	"github.com/m4gshm/gollections/slice/sort"
     9  )
    10  
    11  // By sorts cloned elements slice in ascending order, using the orderConverner function to retrieve a value of type Ordered.
    12  func By[T any, O constraints.Ordered, TS ~[]T](elements TS, orderConverner func(T) O) TS {
    13  	return sort.By(clone.Of(elements), orderConverner)
    14  }
    15  
    16  // DescBy sorts cloned elements slice in descending order, using the orderConverner function to retrieve a value of type Ordered.
    17  func DescBy[T any, O constraints.Ordered, TS ~[]T](elements TS, orderConverner func(T) O) TS {
    18  	return sort.DescBy(clone.Of(elements), orderConverner)
    19  }
    20  
    21  // Asc sorts orderable elements ascending
    22  func Asc[T constraints.Ordered, TS ~[]T](elements TS) TS {
    23  	return sort.Asc(clone.Of(elements))
    24  }
    25  
    26  // Desc sorts orderable elements descending
    27  func Desc[T constraints.Ordered, TS ~[]T](elements TS) TS {
    28  	return sort.Desc(clone.Of(elements))
    29  }