github.com/godaddy-x/freego@v1.0.156/utils/concurrent/sorter.go (about)

     1  package concurrent
     2  
     3  import "sort"
     4  
     5  type Sorter struct {
     6  	item []interface{}
     7  	call func(a, b interface{}) bool
     8  }
     9  
    10  func NewSorter(item []interface{}, call func(a, b interface{}) bool) Sorter {
    11  	return Sorter{item, call}
    12  }
    13  
    14  func (self *Sorter) Len() int {
    15  	return len(self.item)
    16  }
    17  
    18  func (self Sorter) Less(i, j int) bool {
    19  	return self.call(self.item[i], self.item[j])
    20  }
    21  
    22  func (self Sorter) Swap(i, j int) {
    23  	self.item[i], self.item[j] = self.item[j], self.item[i]
    24  }
    25  
    26  func (self Sorter) Sort() []interface{} {
    27  	sort.Sort(&self)
    28  	return self.item
    29  }