Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Improve database multi-select field sorting https://github.com/siyuan-note/siyuan/issues/11062

Danielb21243c77388
diff --git a/kernel/av/sort.go b/kernel/av/sort.go
index 97ccb11565..829e02604b 100644
--- a/kernel/av/sort.go
+++ b/kernel/av/sort.go
@@ -120,36 +120,49 @@ func (value *Value) Compare(other *Value, attrView *AttributeView) int {
}
case KeyTypeSelect, KeyTypeMSelect:
if nil != value.MSelect && nil != other.MSelect {
- var v1 string
- for _, v := range value.MSelect {
- v1 += v.Content
- break
- }
- var v2 string
- for _, v := range other.MSelect {
- v2 += v.Content
- break
- }
-
// 按设置的选项顺序排序
key, _ := attrView.GetKey(value.KeyID)
+ optionSort := map[string]int{}
if nil != key {
- optionSort := map[string]int{}
for i, op := range key.Options {
optionSort[op.Name] = i
}
+ }
- v1Sort := optionSort[v1]
- v2Sort := optionSort[v2]
- if v1Sort > v2Sort {
- return 1
+ vLen := len(value.MSelect)
+ oLen := len(other.MSelect)
+ if vLen <= oLen {
+ for i := 0; i < vLen; i++ {
+ v := value.MSelect[i].Content
+ o := other.MSelect[i].Content
+ vSort := optionSort[v]
+ oSort := optionSort[o]
+ if vSort != oSort {
+ return vSort - oSort
+ }
+ s := strings.Compare(v, o)
+ if 0 != s {
+ return s
+ }
}
- if v1Sort < v2Sort {
- return -1
+ return 1
+ } else {
+ for i := 0; i < oLen; i++ {
+ v := value.MSelect[i].Content
+ o := other.MSelect[i].Content
+ vSort := optionSort[v]
+ oSort := optionSort[o]
+ if vSort != oSort {
+ return vSort - oSort
+ }
+ s := strings.Compare(v, o)
+ if 0 != s {
+ return s
+ }
}
- return 0
+ return -1
}
- return strings.Compare(v1, v2)
+
}
case KeyTypeURL:
if nil != value.URL && nil != other.URL {