-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.go
104 lines (95 loc) · 1.91 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
103
104
package columns
import (
"math"
"sort"
"strings"
)
// Sort the lines base on one (or more) columns
//
// Example: Sort(-1) will sort the 1st column descending;
// Sort(2,3) will sort on column 2 and, if necessary, column 3
//
// In a column with mixed datatypes (strings & numerical), numerical values are grouped 1st, strings 2nd, nil are always last
// regardless of sorting ascending or descending
func (cw *Writer) Sort(columns ...int) {
var sorter = func(i, j int) bool {
for _, c := range columns {
var asc = c > 0
var colIndex = int(abs(int64(c))) - 1
if colIndex < len(cw.columns) {
a := cw.data[i][colIndex]
b := cw.data[j][colIndex]
switch compare(a, b, asc) {
case compareSwap:
return false
case compareKeep:
return true
}
}
}
return false
}
sort.SliceStable(cw.data, sorter)
}
type swap int
const (
compareSwap swap = 1
compareEqual swap = 2
compareKeep swap = 3
)
func compareValue(a, b *CellData, asc bool) int {
switch x := a.value.(type) {
case string:
switch y := b.value.(type) {
case string:
return strings.Compare(x, y)
default:
if asc {
return 1 // swap
}
return -1
}
}
switch b.value.(type) {
case string:
if asc {
return -1 // swap
}
return 1
}
n, ok1 := getNum(a.value)
m, ok2 := getNum(b.value)
if ok1 && ok2 {
diff := n - m
if math.Abs(diff) < 1e-9 {
return 0
}
if diff > 0 {
return 1
}
return -1
}
if !ok2 {
return 1
}
return 0
}
func compare(a, b *CellData, asc bool) swap {
if a.isEmpty() && b.isEmpty() {
return compareKeep // both empty -> dont swap
}
if a.isEmpty() || b.isEmpty() {
if a.isEmpty() {
return compareSwap // one empty -> swap if 1st is empty (promote actual values)
}
return compareKeep
}
comp := compareValue(a, b, asc)
if comp == 0 {
return compareEqual
}
if (asc && comp > 0) || (!asc && comp < 0) {
return compareSwap
}
return compareKeep
}