github.com/gogf/gf@v1.16.9/container/garray/garray_func.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package garray 8 9 import "strings" 10 11 // apiInterfaces is used for type assert api for Interfaces. 12 type apiInterfaces interface { 13 Interfaces() []interface{} 14 } 15 16 // defaultComparatorInt for int comparison. 17 func defaultComparatorInt(a, b int) int { 18 if a < b { 19 return -1 20 } 21 if a > b { 22 return 1 23 } 24 return 0 25 } 26 27 // defaultComparatorStr for string comparison. 28 func defaultComparatorStr(a, b string) int { 29 return strings.Compare(a, b) 30 } 31 32 // quickSortInt is the quick-sorting algorithm implements for int. 33 func quickSortInt(values []int, comparator func(a, b int) int) { 34 if len(values) <= 1 { 35 return 36 } 37 mid, i := values[0], 1 38 head, tail := 0, len(values)-1 39 for head < tail { 40 if comparator(values[i], mid) > 0 { 41 values[i], values[tail] = values[tail], values[i] 42 tail-- 43 } else { 44 values[i], values[head] = values[head], values[i] 45 head++ 46 i++ 47 } 48 } 49 values[head] = mid 50 quickSortInt(values[:head], comparator) 51 quickSortInt(values[head+1:], comparator) 52 } 53 54 // quickSortStr is the quick-sorting algorithm implements for string. 55 func quickSortStr(values []string, comparator func(a, b string) int) { 56 if len(values) <= 1 { 57 return 58 } 59 mid, i := values[0], 1 60 head, tail := 0, len(values)-1 61 for head < tail { 62 if comparator(values[i], mid) > 0 { 63 values[i], values[tail] = values[tail], values[i] 64 tail-- 65 } else { 66 values[i], values[head] = values[head], values[i] 67 head++ 68 i++ 69 } 70 } 71 values[head] = mid 72 quickSortStr(values[:head], comparator) 73 quickSortStr(values[head+1:], comparator) 74 }