10 examples of 'go sort' in Go

Every line of 'go sort' 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
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}
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}
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}
21func (v *ListStore) Sort(compareFunc CompareDataFunc) {
22 C._g_list_store_sort(v.native(), C.gpointer(callback.Assign(compareFunc)))
23}
68func (n *MapLayerEndpoint) Sort(params []*m.SortMapLayer) (err error) {
69
70 for _, s := range params {
71 n.adaptors.MapLayer.Sort(&m.MapLayer{
72 Id: s.Id,
73 Weight: s.Weight,
74 })
75 }
76
77 return
78}
22func sort(a, b, c int) (int, int, int) {
23 if c < a {
24 a, c = c, a
25 }
26 if b < a {
27 a, b = b, a
28 }
29 if c < b {
30 b, c = c, b
31 }
32 return a, b, c
33}
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}
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}
72func (blocks Blocks) Sort() {
73 sort.Slice(blocks, func(i, j int) bool {
74 return blocks[i].Behind(blocks[j])
75 })
76}
453func (g *DirectCandidateGenerator) Sort(sort string) *DirectCandidateGenerator {
454 g.sort = &sort
455 return g
456}

Related snippets