You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
824 B

4 years ago
  1. package schedule
  2. import (
  3. "sort"
  4. )
  5. /** 用于定义各类排序 **/
  6. //通用结构体排序,必须重写数组Len() Swap() Less()函数
  7. type body_wrapper struct {
  8. Bodys [] interface{}
  9. by func(p,q*interface{}) bool //内部Less()函数会用到
  10. }
  11. type SortBodyBy func(p, q* interface{}) bool //定义一个函数类型
  12. //数组长度Len()
  13. func (acw body_wrapper) Len() int {
  14. return len(acw.Bodys)
  15. }
  16. //元素交换
  17. func (acw body_wrapper) Swap(i,j int){
  18. acw.Bodys[i],acw.Bodys[j] = acw.Bodys[j],acw.Bodys[i]
  19. }
  20. //比较函数,使用外部传入的by比较函数
  21. func (acw body_wrapper) Less(i,j int) bool {
  22. return acw.by(&acw.Bodys[i],&acw.Bodys[j])
  23. }
  24. //自定义排序字段,参考SortBodyByCreateTime中的传入函数
  25. func SortBody(bodys [] interface{}, by SortBodyBy){
  26. sort.Sort(body_wrapper{bodys,by})
  27. }