SortKeys sorts a list of map keys, deduplicating keys if necessary. The type of each value must be comparable.
(vs []reflect.Value)
| 14 | // SortKeys sorts a list of map keys, deduplicating keys if necessary. |
| 15 | // The type of each value must be comparable. |
| 16 | func SortKeys(vs []reflect.Value) []reflect.Value { |
| 17 | if len(vs) == 0 { |
| 18 | return vs |
| 19 | } |
| 20 | |
| 21 | // Sort the map keys. |
| 22 | sort.SliceStable(vs, func(i, j int) bool { return isLess(vs[i], vs[j]) }) |
| 23 | |
| 24 | // Deduplicate keys (fails for NaNs). |
| 25 | vs2 := vs[:1] |
| 26 | for _, v := range vs[1:] { |
| 27 | if isLess(vs2[len(vs2)-1], v) { |
| 28 | vs2 = append(vs2, v) |
| 29 | } |
| 30 | } |
| 31 | return vs2 |
| 32 | } |
| 33 | |
| 34 | // isLess is a generic function for sorting arbitrary map keys. |
| 35 | // The inputs must be of the same type and must be comparable. |