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{})
| 26 | // Everything else is sorted lexicographically by first converting to string |
| 27 | // with Sprintf. |
| 28 | func 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. |