10 examples of 'golang sort map' in Go

Every line of 'golang sort map' 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
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}
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

46func SortMapByKey(m map[rune]int, reverse bool) RuneIntPairList {
47 sm := make(RuneIntPairList, len(m))
48 i := 0
49 for k, v := range m {
50 sm[i] = RuneIntPair{k, v}
51 i++
52 }
53 if !reverse {
54 sort.Sort(sm)
55 } else {
56 sort.Sort(reverseAdapter{Interface: sm})
57 }
58 return sm
59}
60func sortKeys(v []reflect.Value, order string) []reflect.Value {
61 if len(v) <= 1 {
62 return v
63 }
64
65 switch v[0].Kind() {
66 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
67 if order == "desc" {
68 sort.Sort(sort.Reverse(mapKeyByInt{v}))
69 } else {
70 sort.Sort(mapKeyByInt{v})
71 }
72 case reflect.String:
73 if order == "desc" {
74 sort.Sort(sort.Reverse(mapKeyByStr{v}))
75 } else {
76 sort.Sort(mapKeyByStr{v})
77 }
78 }
79 return v
80}
212func sortMapByValueDesc(m map[int64]float64) pairList {
213 p := make(pairList, len(m))
214 i := 0
215 for k, v := range m {
216 p[i] = pair{k, v}
217 i++
218 }
219 sort.Sort(sort.Reverse(p))
220 return p
221}
85func printMapOrdered(m map[string]string) {
86 keys := make([]string, 0, len(m))
87 for k := range m {
88 keys = append(keys, k)
89 }
90 sort.Strings(keys)
91
92 for _, k := range keys {
93 fmt.Println(k+":", m[k])
94 }
95}
14func quickSort(A []int) {
15 recursionSort(A, 0, len(A)-1)
16}
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}
18func SortMapString(obj map[string]interface{}) map[string]interface{} {
19 mk := make([]string, len(obj))
20
21 i := 0
22 for k, _ := range obj {
23 mk[i] = k
24 i++
25 }
26
27 sort.Strings(mk)
28
29 results := make(map[string]interface{})
30
31 for _, index := range mk {
32 results[index] = obj[index]
33 }
34
35 return results
36}
78func (this *MapValue) getMapSortedKey() []string {
79 var unsortKey []string
80 for k := range this.Data {
81 unsortKey = append(unsortKey, k)
82 }
83 sort.Strings(unsortKey)
84 return unsortKey
85}

Related snippets