-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_sort.go
61 lines (46 loc) · 1.34 KB
/
data_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
package series
import (
"sort"
)
type DTypeSlice []DType
func (x DTypeSlice) Len() int { return len(x) }
func (x DTypeSlice) Less(i, j int) bool {
return x[i] < x[j] || (IsNA(x[i]) && !IsNA(x[j]))
}
func (x DTypeSlice) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
type sortable Data
func (x sortable) Len() int { return len(x.values) }
func (x sortable) Less(i, j int) bool {
return x.values[i] < x.values[j] || (IsNA(x.values[i]) && !IsNA(x.values[j]))
}
func (x sortable) Swap(i, j int) {
x.values[i], x.values[j] = x.values[j], x.values[i]
x.index[i], x.index[j] = x.index[j], x.index[i]
}
type argSortable Data
func (x argSortable) Len() int { return len(x.index) }
func (x argSortable) Less(i, j int) bool { return x.index[i] < x.index[j] }
func (x argSortable) Swap(i, j int) {
x.values[i], x.values[j] = x.values[j], x.values[i]
x.index[i], x.index[j] = x.index[j], x.index[i]
}
// IndexSort sorts data's index.
func (d Data) IndexSort() Data {
sort.Sort(argSortable(d))
return d
}
// Sort sorts data.
func (d Data) Sort() Data {
sort.Sort(sortable(d))
return d
}
// IndexSortStable sorts data's index using stable sort algorithm.
func (d Data) IndexSortStable() Data {
sort.Stable(argSortable(d))
return d
}
// SortStable sorts data's index using stable sort algorithm.
func (d Data) SortStable() Data {
sort.Stable(sortable(d))
return d
}