MCPcopy Create free account
hub / github.com/google/gapid / Sort

Function Sort

core/data/slice/sort.go:28–57  ·  view source on GitHub ↗

Sort sorts the elements of the slice s into ascending order. Numbers are sorted numerically. Booleans are sorted with false before true. Everything else is sorted lexicographically by first converting to string with Sprintf.

(s interface{})

Source from the content-addressed store, hash-verified

26// Everything else is sorted lexicographically by first converting to string
27// with Sprintf.
28func Sort(s interface{}) {
29 v := getSlice(s)
30 switch v.Type().Elem().Kind() {
31 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
32 sort.Slice(s, func(i, j int) bool {
33 return v.Index(i).Int() < v.Index(j).Int()
34 })
35 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
36 sort.Slice(s, func(i, j int) bool {
37 return v.Index(i).Uint() < v.Index(j).Uint()
38 })
39 case reflect.Float32, reflect.Float64:
40 sort.Slice(s, func(i, j int) bool {
41 return v.Index(i).Float() < v.Index(j).Float()
42 })
43 case reflect.Bool:
44 sort.Slice(s, func(i, j int) bool {
45 return !v.Index(i).Bool() && v.Index(j).Bool()
46 })
47 case reflect.String:
48 sort.Slice(s, func(i, j int) bool {
49 return v.Index(i).String() < v.Index(j).String()
50 })
51 default:
52 sort.Slice(s, func(i, j int) bool {
53 a, b := v.Index(i).Interface(), v.Index(j).Interface()
54 return fmt.Sprint(a) < fmt.Sprint(b)
55 })
56 }
57}
58
59// SortValues sorts the reflect.Values in the slice s into ascending order.
60// All the elements of e need to be of the type elTy.

Callers

nothing calls this directly

Calls 11

getSliceFunction · 0.85
InterfaceMethod · 0.80
SprintMethod · 0.80
KindMethod · 0.65
TypeMethod · 0.65
SliceMethod · 0.65
IndexMethod · 0.65
BoolMethod · 0.65
StringMethod · 0.65
IntMethod · 0.45
UintMethod · 0.45

Tested by

no test coverage detected