From 919cd04c8a33d624b4cabb99e4cfdb1059fcbc87 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Mon, 3 Jul 2023 21:16:38 +0800 Subject: [PATCH] :art: Update av --- kernel/model/attribute_view.go | 22 +++++++++++++--------- kernel/util/misc.go | 12 ------------ 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index a2e1985c6..79a09a8af 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -58,7 +58,6 @@ func filterRows(ret *av.AttributeView) { } } } - colIndexes = util.RemoveDuplicatedElem(colIndexes) var rows []*av.Row for _, row := range ret.Rows { @@ -84,32 +83,37 @@ func filterRows(ret *av.AttributeView) { func sortRows(ret *av.AttributeView) { if 0 < len(ret.Sorts) { - var colIndexes []int + type ColIndexSort struct { + Index int + Order av.SortOrder + } + + var colIndexSorts []*ColIndexSort for _, s := range ret.Sorts { for i, c := range ret.Columns { if c.ID == s.Column { - colIndexes = append(colIndexes, i) + colIndexSorts = append(colIndexSorts, &ColIndexSort{Index: i, Order: s.Order}) break } } } - colIndexes = util.RemoveDuplicatedElem(colIndexes) sort.Slice(ret.Rows, func(i, j int) bool { - for _, index := range colIndexes { - c := ret.Columns[index] + for _, colIndexSort := range colIndexSorts { + c := ret.Columns[colIndexSort.Index] if c.Type == av.ColumnTypeBlock { continue } - result := ret.Rows[i].Cells[index].Value.Compare(ret.Rows[j].Cells[index].Value) + result := ret.Rows[i].Cells[colIndexSort.Index].Value.Compare(ret.Rows[j].Cells[colIndexSort.Index].Value) if 0 == result { continue } - if 0 > result { - return true + if colIndexSort.Order == av.SortOrderAsc { + return 0 > result } + return 0 < result } return false }) diff --git a/kernel/util/misc.go b/kernel/util/misc.go index feb3f4fcf..ad047e473 100644 --- a/kernel/util/misc.go +++ b/kernel/util/misc.go @@ -24,18 +24,6 @@ import ( "github.com/88250/lute/html" ) -// RemoveDuplicatedElem removes the duplicated elements from the slice. -func RemoveDuplicatedElem(slice []int) (ret []int) { - allKeys := make(map[int]bool) - for _, item := range slice { - if _, value := allKeys[item]; !value { - allKeys[item] = true - ret = append(ret, item) - } - } - return -} - // InsertElem inserts a new element value at the specified index position. // 0 <= index <= len(a) func InsertElem[T any](ret []T, index int, value T) []T {