// Sort sorts data.
//
// It makes one call to data.Len to determine n, and O(n*log(n)) calls to
// data.Less and data.Swap. The sort is not guaranteed to be stable.
//
// To sort slices without creating a type, see Slice.
167178
func Sort(data Interface) {
	n := data.Len()
	if fs, ok := data.(*funcs); ok {
		quickSort_func(fs.lessSwap, 0, n, maxDepth(n))
	} else {
		quickSort(data, 0, n, maxDepth(n))
	}
}
399635
func (h *BytesRefHash) Sort(comp func(a, b []byte) bool) []int {
	compact := h.compact()
	s := newBytesRefIntroSorter(h, compact, comp)
	s.Sort(0, h.count)
	// TODO remove this
	// for i, _ := range compact {
	// 	if compact[i+1] == -1 {
	// 		break
	// 	}
	// 	assert(!s.Less(i+1, i))
	// 	if ok := !s.Less(i+1, i); !ok {
	// 		fmt.Println("DEBUG1", compact)
	// 		assert(ok)
	// 	}
	// }
	return compact
}
641695
func (ms *multiAddrSorter) Sort(sockAddrs SockAddrs) {
	ms.addrs = sockAddrs
	sort.Sort(ms)
}
719076
func (ns *Namespace) Sort(seq interface{}, args ...interface{}) (interface{}, error) {
	if seq == nil {
		return nil, errors.New("sequence must be provided")
	}

	seqv := reflect.ValueOf(seq)
	seqv, isNil := indirect(seqv)
	if isNil {
		return nil, errors.New("can't iterate over a nil value")
	}

	switch seqv.Kind() {
	case reflect.Array, reflect.Slice, reflect.Map:
		// ok
	default:
		return nil, errors.New("can't sort " + reflect.ValueOf(seq).Type().String())
	}

	// Create a list of pairs that will be used to do the sort
	p := pairList{SortAsc: true, SliceType: reflect.SliceOf(seqv.Type().Elem())}
	p.Pairs = make([]pair, seqv.Len())

	var sortByField string
	for i, l := range args {
		dStr, err := cast.ToStringE(l)
		switch {
		case i == 0 && err != nil:
			sortByField = ""
		case i == 0 && err == nil:
			sortByField = dStr
		case i == 1 && err == nil && dStr == "desc":
			p.SortAsc = false
		case i == 1:
			p.SortAsc = true
		}
	}
	path := strings.Split(strings.Trim(sortByField, "."), ".")

	switch seqv.Kind() {
	case reflect.Array, reflect.Slice:
		for i := 0; i < seqv.Len(); i++ {
			p.Pairs[i].Value = seqv.Index(i)
			if sortByField == "" || sortByField == "value" {
				p.Pairs[i].Key = p.Pairs[i].Value
			} else {
				v := p.Pairs[i].Value
				var err error
				for _, elemName := range path {
					v, err = evaluateSubElem(v, elemName)
					if err != nil {
						return nil, err
					}
				}
				p.Pairs[i].Key = v
			}
		}

	case reflect.Map:
		keys := seqv.MapKeys()
		for i := 0; i < seqv.Len(); i++ {
			p.Pairs[i].Value = seqv.MapIndex(keys[i])
			if sortByField == "" {
				p.Pairs[i].Key = keys[i]
			} else if sortByField == "value" {
				p.Pairs[i].Key = p.Pairs[i].Value
			} else {
				v := p.Pairs[i].Value
				var err error
				for _, elemName := range path {
					v, err = evaluateSubElem(v, elemName)
					if err != nil {
						return nil, err
					}
				}
				p.Pairs[i].Key = v
			}
		}
	}
	return p.sort(), nil
}
364068
func Sort(list cty.Value) (cty.Value, error) {
	return SortFunc.Call([]cty.Value{list})
}
776374
func sort(s1, s2 string) (shorter, longer string) {
	if utf8.RuneCountInString(s1) < utf8.RuneCountInString(s2) {
		return s1, s2
	}
	return s2, s1
}
906922
func (r *Redis) Sort(key string) *SortCommand {
	return &SortCommand{redis: r, key: key}
}
602151
func (ms *multiSorter) Sort(stats []ExprStats) {
	ms.stats = stats
	sort.Sort(ms)
}
1053643
func Sort(g Iterator) *Immutable {
	if g, ok := g.(*Immutable); ok {
		return g
	}
	return build(g, false)
}
432179
func (o Oids) Sort() Oids {
	c := make(Oids, len(o))
	copy(c, o)
	sort.Sort(sortableOids{c})
	return c
}
1137056
func SortBy(rows [][]string, lesses ...LessFunc) *MultiSorter {
	return &MultiSorter{
		data: rows,
		less: lesses,
	}
}
819588
func (c *TasksRequestsCall) Sort(sort string) *TasksRequestsCall {
	c.urlParams_.Set("sort", sort)
	return c
}
1289996
func (by pageBy) Sort(pages Pages) {
	ps := &pageSorter{
		pages: pages,
		by:    by, // The Sort method's receiver is the function (closure) that defines the sort order.
	}
	sort.Stable(ps)
}
473736
func Sort(data interface{}, identifier ...func(interface{}) int) {
	val := reflect.ValueOf(data)
	identifier = append(identifier, DefaultID)
	sortable := byInt{
		Data:       val,
		Indices:    make([]int, val.Len()),
		Identifier: identifier[0],
	}
	for i := 0; i < val.Len(); i++ {
		sortable.Indices[i] = i
	}
	sort.Sort(sortable)
	return
}
58662
func (s *DeleteByQueryService) Sort(sort ...string) *DeleteByQueryService {
	s.sort = append(s.sort, sort...)
	return s
}
1275987
func Slice(slicePtr interface{}, less func(i, j int) bool) {
	v := reflect.ValueOf(slicePtr).Elem()
	if v.Len() <= 1 {
		return
	}
	sort.Slice(v.Interface(), less)

	i := 0
	for j := 1; j < v.Len(); j++ {
		if !less(i, j) {
			continue
		}
		i++
		v.Index(i).Set(v.Index(j))
	}
	i++
	v.SetLen(i)
}
169330
func (v Versions) Sort() {
	sort.Slice(v, func(i, j int) bool {
		return v[i].NewerThan(v[j])
	})
}