SortValues sorts the reflect.Values in the slice s into ascending order. All the elements of e need to be of the type elTy. Numbers are sorted numerically. Booleans are sorted with false before true. Everything else is sorted lexicographically by first converting to string with Sprintf.
(s []reflect.Value, elTy reflect.Type)
| 63 | // Everything else is sorted lexicographically by first converting to string |
| 64 | // with Sprintf. |
| 65 | func SortValues(s []reflect.Value, elTy reflect.Type) { |
| 66 | switch elTy.Kind() { |
| 67 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 68 | sort.Slice(s, func(i, j int) bool { |
| 69 | return s[i].Int() < s[j].Int() |
| 70 | }) |
| 71 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 72 | sort.Slice(s, func(i, j int) bool { |
| 73 | return s[i].Uint() < s[j].Uint() |
| 74 | }) |
| 75 | case reflect.Float32, reflect.Float64: |
| 76 | sort.Slice(s, func(i, j int) bool { |
| 77 | return s[i].Float() < s[j].Float() |
| 78 | }) |
| 79 | case reflect.Bool: |
| 80 | sort.Slice(s, func(i, j int) bool { |
| 81 | return !s[i].Bool() && s[j].Bool() |
| 82 | }) |
| 83 | case reflect.String: |
| 84 | sort.Slice(s, func(i, j int) bool { |
| 85 | return s[i].String() < s[j].String() |
| 86 | }) |
| 87 | default: |
| 88 | sort.Slice(s, func(i, j int) bool { |
| 89 | a, b := s[i].Interface(), s[j].Interface() |
| 90 | return fmt.Sprint(a) < fmt.Sprint(b) |
| 91 | }) |
| 92 | } |
| 93 | } |