1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| func QuickSortAll(a []interface{}, left, right int, By func(a, b interface{}) bool) []interface{} { if left < right { mid := partitionAll(a, left, right, By) QuickSortAll(a, left, mid-1, By) QuickSortAll(a, mid+1, right, By) } return a
}
func partitionAll(a []interface{}, left int, right int, By func(a, b interface{}) bool) int { pivot := a[left] for ; left < right; { for ; left < right && By(a[right], pivot); { right-- } a[left] = a[right] for ; left < right && By(pivot, a[left]); { left++
} a[right] = a[left] } a[left] = pivot return left }
type User struct { Name string Age int Count int }
const counter = 10
const ( flagResult = false SortLow = - 1 SortEquals = 0 SortHigh = 1 )
var SortingRules = map[string]bool{"Name": true}
func main() {
isp := make([]interface{}, 0, counter)
for i := 0; i < counter; i++ { isp = append(isp, User{ Name: strconv.Itoa(rand.Intn(counter)), Age: i, Count: rand.Intn(counter), })
} fmt.Println(isp) t := time.Now().Unix() num := QuickSortAll(isp, 0, len(isp)-1, func(a, b interface{}) bool { if reflect.ValueOf(a).Kind() != reflect.ValueOf(b).Kind() { return !flagResult } return sortByRules(a, b, SortingRules)
}) fmt.Println(num) fmt.Println("耗时:",time.Now().Unix() - t) }
func sortByRules(a, b interface{}, sortingRules map[string]bool) bool { ofType := reflect.ValueOf(a).Kind() va := reflect.ValueOf(a) vb := reflect.ValueOf(b)
for v, k := range sortingRules {
if ofType != reflect.Struct { result := typeChooseOfReflect(va, vb, k) if result == SortEquals { continue } else if result < SortEquals { return flagResult } else { return !flagResult } } else if ofType == reflect.Struct { of := va.FieldByName(v) ofb := vb.FieldByName(v)
if of.Kind() == reflect.Struct || ofb.Kind() == reflect.Struct { return !flagResult }
if !of.IsValid() || !ofb.IsValid() { return !flagResult }
result := typeChooseOfReflect(of, ofb, k) if result == SortEquals { continue } else if result < SortEquals { return flagResult } else { return !flagResult } } } return !flagResult }
func typeChooseOfReflect(valueOfA, valueOfB reflect.Value, flag bool) int8 { if flag { valueOfA, valueOfB = valueOfB, valueOfA } kind := valueOfA.Type().Kind() switch kind { case reflect.Bool: return SortHigh
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: if valueOfA.Int() < valueOfB.Int() { return SortHigh } if valueOfA.Int() == valueOfB.Int() { return SortEquals }
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: if valueOfA.Uint() < valueOfB.Uint() { return SortHigh } if valueOfA.Uint() == valueOfB.Uint() { return SortEquals }
case reflect.Float32, reflect.Float64: if valueOfA.Float() < valueOfB.Float() { return SortHigh } if valueOfA.Float() == valueOfB.Float() { return SortEquals }
case reflect.String: return -int8(strings.Compare(valueOfA.String(), valueOfB.String())) } return SortLow }
|