2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-18 15:25:31 +00:00
cointop/pkg/table/row.go
Miguel Mota 77f5c752e9 build script
Former-commit-id: 807a38499b5e912a909d18e142ac5ffd27518f47 [formerly 807a38499b5e912a909d18e142ac5ffd27518f47 [formerly 3b23f739d91859165827c1af6a923cf7be19a1d5 [formerly 6f3f3dd824]]]
Former-commit-id: 36948040aa5bec0dd2c25ec210e02349f5bddf0a
Former-commit-id: 4b1b9d17fc425bc764a46d0fdcd12747cca608f5 [formerly 56d4451c69972aa25ed17119773e6af9085b00c1]
Former-commit-id: 7967040e3a6348128010d3bc0ad213e5eabbf567
2018-03-31 01:18:53 -07:00

69 lines
1.3 KiB
Go

package table
// Row row
type Row struct {
table *Table
values []interface{}
strValues []string
}
// Rows rows
type Rows []*Row
// Len count
func (r Rows) Len() int {
return len(r)
}
// Swap swap rows
func (r Rows) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
// Less less
func (r Rows) Less(i, j int) bool {
sortOrder := r[i].table.sort
var k int
for k = 0; k < len(sortOrder)-1; k++ {
s := sortOrder[k]
if s.order == SortDesc {
if gt(r[i].values[s.index], r[j].values[s.index], s.sortFn) {
return true
}
if gt(r[j].values[s.index], r[i].values[s.index], s.sortFn) {
return false
}
} else {
if lt(r[i].values[s.index], r[j].values[s.index], s.sortFn) {
return true
}
if lt(r[j].values[s.index], r[i].values[s.index], s.sortFn) {
return false
}
}
}
s := sortOrder[k]
if s.order == SortDesc {
return gt(r[i].values[s.index], r[j].values[s.index], s.sortFn)
}
return lt(r[i].values[s.index], r[j].values[s.index], s.sortFn)
}
func gt(a interface{}, b interface{}, fn SortFn) bool {
return lt(b, a, fn)
}
func lt(a interface{}, b interface{}, fn SortFn) bool {
if fn != nil {
return fn(a, b)
}
switch a.(type) {
case int:
return a.(int) < b.(int)
case string:
return a.(string) < b.(string)
}
return false
}