-
Notifications
You must be signed in to change notification settings - Fork 0
/
orders.go
77 lines (63 loc) · 1.46 KB
/
orders.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
package godf
import (
"fmt"
"math/rand"
"sort"
"time"
)
// Sort sorts the data according to a certain column. The column must be numeric
//
// Example of usage:
// df.Sort("x")
// this will sort all the table, according to the column x
func (d *dataframe) Sort(header string) {
df := d.Transpose()
colNum := -1
for i, h := range d.headers {
if h == header {
colNum = i
}
}
if colNum == -1 {
fmt.Printf("godf.Sort: column name %s not found\n", header)
return
}
df.data = sortByColumn(df.data, colNum)
df = df.Transpose()
d.data = df.data
}
func sortByColumn(data [][]interface{}, columnIndex int) [][]interface{} {
sort.Slice(data, func(i, j int) bool {
valueI := data[i][columnIndex]
valueJ := data[j][columnIndex]
if i, ok := valueI.(int); ok {
valueI = float64(i)
}
if j, ok := valueJ.(int); ok {
valueJ = float64(j)
}
// Assuming the column values are of type float64 for simplicity
return valueI.(float64) < valueJ.(float64)
})
return data
}
// Scramble will shuffle the table randomly. A seed can be send
// but if not, it will be randomly generated
//
// Example of usage:
// df.Scramble(1000)
func (d *dataframe) Scramble(seed ...int) {
if len(seed) > 0 {
rand.Seed(int64(seed[0]))
} else {
rand.Seed(time.Now().UnixNano())
}
df := d.Transpose()
n := len(df.data)
for i := n - 1; i > 0; i-- {
j := rand.Intn(i + 1)
df.data[i], df.data[j] = df.data[j], df.data[i]
}
df = df.Transpose()
d.data = df.data
}