forked from unidoc/unioffice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompare.go
94 lines (80 loc) · 2.27 KB
/
compare.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
// Copyright 2017 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting [email protected].
package spreadsheet
import (
"strconv"
"github.com/unidoc/unioffice/spreadsheet/reference"
)
// SortOrder is a column sort order.
//go:generate stringer -type=SortOrder
type SortOrder byte
// SortOrder constants
const (
SortOrderAscending SortOrder = iota
SortOrderDescending
)
// Comparer is used to compare rows based off a column and cells based off of
// their value.
type Comparer struct {
Order SortOrder
}
// LessRows compares two rows based off of a column. If the column doesn't exist
// in one row, that row is 'less'.
func (c Comparer) LessRows(column string, lhs, rhs Row) bool {
var lhsCell, rhsCell Cell
for _, c := range lhs.Cells() {
cref, _ := reference.ParseCellReference(c.Reference())
if cref.Column == column {
lhsCell = c
break
}
}
for _, c := range rhs.Cells() {
cref, _ := reference.ParseCellReference(c.Reference())
if cref.Column == column {
rhsCell = c
break
}
}
return c.LessCells(lhsCell, rhsCell)
}
// LessCells returns true if the lhs value is less than the rhs value. If the
// cells contain numeric values, their value interpreted as a floating point is
// compared. Otherwise their string contents are compared.
func (c Comparer) LessCells(lhs, rhs Cell) bool {
if c.Order == SortOrderDescending {
lhs, rhs = rhs, lhs
}
// handle zero-value cells first as we can get those based off of LessRows
// above
if lhs.X() == nil {
if rhs.X() == nil {
return false
}
return true
}
if rhs.X() == nil {
return false
}
lhsValue, lhsIsNum := lhs.getRawSortValue()
rhsValue, rhsIsNum := rhs.getRawSortValue()
switch {
// both numbers
case lhsIsNum && rhsIsNum:
lf, _ := strconv.ParseFloat(lhsValue, 64)
rf, _ := strconv.ParseFloat(rhsValue, 64)
return lf < rf
// numbers sort before non-numbers
case lhsIsNum:
return true
case rhsIsNum:
return false
}
lhsValue = lhs.GetFormattedValue()
rhsValue = rhs.GetFormattedValue()
return lhsValue < rhsValue
}