10 examples of 'golang sort slice' in Go

Every line of 'golang sort slice' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
32func (slice UIntPtrSlice) SortAsc() UIntPtrSlice {
33 sort.SliceStable(slice, func(i, j int) bool {
34 return slice[i] < slice[j]
35 })
36 return slice
37}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

7func Sort(arr []int) []int{
8 if len(arr) <= 1 {
9 return arr
10 }
11
12 n := len(arr) - 1
13
14 for {
15 if n == 0 {
16 break
17 }
18
19 for i := 0; i < len(arr)-1; i++ {
20 if arr[i] > arr[i+1] {
21 arr[i], arr[i+1] = arr[i+1], arr[i]
22 }
23 }
24 n -= 1
25 }
26
27 return arr
28}
17func Sort(arr []int) []int {
18 for i := 0; i < len(arr); i++ {
19 min := i
20 for j := i + 1; j < len(arr); j++ {
21 if arr[j] < arr[min] {
22 min = j
23 }
24 }
25 if min != i {
26 arr[i], arr[min] = arr[min], arr[i]
27 }
28 }
29 return arr
30}
6func GnomeSort(arr []int) []int {
7 for pos := 1; pos < len(arr); pos++ {
8 if arr[pos] >= arr[pos-1] {
9 continue
10 } else {
11 arr[pos], arr[pos-1] = arr[pos-1], arr[pos]
12 if pos > 1 {
13 pos -= 1
14 }
15 }
16 pos -= 1
17 }
18
19 return arr
20}
30func quickSortUtil(arr []int, lower int, upper int) {
31 if upper <= lower {
32 return
33 }
34 pivot := arr[lower]
35 start := lower
36 stop := upper
37
38 for lower < upper {
39 for arr[lower] <= pivot && lower < upper {
40 lower++
41 }
42 for arr[upper] > pivot && lower <= upper {
43 upper--
44 }
45 if lower < upper {
46 swap(arr, upper, lower)
47 }
48 }
49 swap(arr, upper, start) // upper is the pivot position
50 quickSortUtil(arr, start, upper-1) // pivot -1 is the upper for left sub array.
51 quickSortUtil(arr, upper+1, stop) // pivot + 1 is the lower for right sub array.
52}
274func SortImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
275 coll := Car(args)
276 if !ListP(coll) {
277 err = ProcessError("sort requires a list as it's first argument.", env)
278 return
279 }
280
281 proc := Cadr(args)
282 if !FunctionOrPrimitiveP(proc) {
283 err = ProcessError("sort requires a function or primitive as it's second argument.", env)
284 return
285 }
286
287 arr := ToArray(coll)
288
289 sort.Slice(arr, func(i, j int) bool {
290 var ret bool
291 if err == nil {
292 a := arr[i]
293 b := arr[j]
294 ret, err = sortCompare(a, b, proc, env)
295 }
296 return ret
297 })
298
299 return ArrayToList(arr), err
300}
14func quickSort(A []int) {
15 recursionSort(A, 0, len(A)-1)
16}
11func quicksort(seq []int) []int {
12 l := len(seq)
13 if l <= 1 {
14 return seq
15 }
16 s := seq[0]
17 left := []int{}
18 right := []int{}
19
20 for i := 1; i < l; i++ {
21 if seq[i] <= s {
22 left = append(left, seq[i])
23 } else {
24 right = append(right, seq[i])
25 }
26 }
27 left = quicksort(left)
28 right = quicksort(right)
29
30 seq = append(left, s)
31 seq = append(seq, right...)
32
33 return seq
34}
11func (ids idList) sort(reverse bool) {
12 compareFunc := func(i, j int) bool {
13 return ids[i].Compare(ids[j])
14 }
15
16 if reverse {
17 compareFunc = func(i, j int) bool {
18 return ids[j].Compare(ids[i])
19 }
20 }
21
22 sort.Slice(ids, compareFunc)
23}
147func (ms migrationSorter) Sort(direction bool) {
148
149 // sort ascending or descending by version
150 if direction {
151 sort.Sort(ms)
152 } else {
153 sort.Sort(sort.Reverse(ms))
154 }
155
156 // now that we're sorted in the appropriate direction,
157 // populate next and previous for each migration
158 for i, m := range ms {
159 prev := int64(-1)
160 if i > 0 {
161 prev = ms[i-1].Version
162 ms[i-1].Next = m.Version
163 }
164 ms[i].Previous = prev
165 }
166}

Related snippets