/
sort.go
102 lines (96 loc) · 2.25 KB
/
sort.go
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
package utils
import (
"fmt"
"github.com/0xVanfer/types"
)
// Simple sort. Do not need to define Len(), Less(), Swap().
//
// Should not be used to sort too much amount of values.
// If the length is over 12, sort.Sort() suggested.
//
// `ascending`: True: The result will be ascending.
// False: The result will be descending.
//
// Example:
//
// SortSimple(true, []int{1, 2, 5, 6, 3, 12}...) = []int{1, 2, 3, 5, 6, 12}
func SortSimple[T types.Ordered](ascending bool, input ...T) []T {
length := len(input)
if length == 0 {
return []T{}
}
if length > 100 {
fmt.Println("Input too long, sort.Sort() suggested.")
}
if length > 1000 {
return []T{}
}
// sort.insertionSort
for i := 1; i < length; i++ {
if ascending {
// Ascending.
for j := i; j > 0 && input[j] < input[j-1]; j-- {
input[j-1], input[j] = input[j], input[j-1]
}
} else {
// Descending.
for j := i; j > 0 && input[j] > input[j-1]; j-- {
input[j-1], input[j] = input[j], input[j-1]
}
}
}
return input
}
// Simple sort by map value. Do not need to define Len(), Less(), Swap().
//
// Should not be used to sort too much amount of values.
// If the length is over 12, sort.Sort() suggested.
//
// Returns the ordered keys and ordered values.
//
// Example:
//
// mapp := map[string]int{
// "a": 1,
// "b": 3,
// "c": 4,
// "d": 2,
// }
// x, y := SortSimpleMap(true, mapp)
// x = []string{"a", "d", "b", "c"}
// y = []int{1, 2, 3, 4}
func SortSimpleMap[S comparable, T types.Ordered](ascending bool, input map[S]T) ([]S, []T) {
length := len(input)
if length == 0 {
return nil, nil
}
if length > 100 {
fmt.Println("Input too long, sort.Sort() suggested.")
}
if length > 1000 {
return nil, nil
}
var xList []S
var yList []T
for x, y := range input {
xList = append(xList, x)
yList = append(yList, y)
}
// sort.insertionSort
for i := 1; i < length; i++ {
if ascending {
// Ascending.
for j := i; j > 0 && yList[j] < yList[j-1]; j-- {
yList[j-1], yList[j] = yList[j], yList[j-1]
xList[j-1], xList[j] = xList[j], xList[j-1]
}
} else {
// Descending.
for j := i; j > 0 && yList[j] > yList[j-1]; j-- {
yList[j-1], yList[j] = yList[j], yList[j-1]
xList[j-1], xList[j] = xList[j], xList[j-1]
}
}
}
return xList, yList
}