♻️ Refactor av data structure

This commit is contained in:
Daniel 2023-07-11 19:45:27 +08:00
parent 2f99364071
commit bd95fdc5d7
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
8 changed files with 724 additions and 582 deletions

View file

@ -16,9 +16,11 @@
package av
import "sort"
type Sortable interface {
SortRows()
}
type AttributeViewSort struct {
type ViewSort struct {
Column string `json:"column"` // 列 ID
Order SortOrder `json:"order"` // 排序顺序
}
@ -29,44 +31,3 @@ const (
SortOrderAsc SortOrder = "ASC"
SortOrderDesc SortOrder = "DESC"
)
func (av *AttributeView) SortRows() {
if 1 > len(av.Sorts) {
return
}
type ColIndexSort struct {
Index int
Order SortOrder
}
var colIndexSorts []*ColIndexSort
for _, s := range av.Sorts {
for i, c := range av.Columns {
if c.ID == s.Column {
colIndexSorts = append(colIndexSorts, &ColIndexSort{Index: i, Order: s.Order})
break
}
}
}
sort.Slice(av.Rows, func(i, j int) bool {
for _, colIndexSort := range colIndexSorts {
c := av.Columns[colIndexSort.Index]
if c.Type == ColumnTypeBlock {
continue
}
result := av.Rows[i].Cells[colIndexSort.Index].Value.Compare(av.Rows[j].Cells[colIndexSort.Index].Value)
if 0 == result {
continue
}
if colIndexSort.Order == SortOrderAsc {
return 0 > result
}
return 0 < result
}
return false
})
}