🐛 Database multi-field sorting fails Fix https://github.com/siyuan-note/siyuan/issues/10616

This commit is contained in:
Daniel 2024-03-15 19:14:57 +08:00
parent 4c174a100c
commit 0daa752ba9
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
2 changed files with 48 additions and 73 deletions

View file

@ -44,19 +44,11 @@ func (value *Value) Compare(other *Value) int {
switch value.Type {
case KeyTypeBlock:
if nil != value.Block && nil != other.Block {
ret := strings.Compare(value.Block.Content, other.Block.Content)
if 0 == ret {
ret = int(value.CreatedAt - other.CreatedAt)
}
return ret
return strings.Compare(value.Block.Content, other.Block.Content)
}
case KeyTypeText:
if nil != value.Text && nil != other.Text {
ret := strings.Compare(value.Text.Content, other.Text.Content)
if 0 == ret {
ret = int(value.CreatedAt - other.CreatedAt)
}
return ret
return strings.Compare(value.Text.Content, other.Text.Content)
}
case KeyTypeNumber:
if nil != value.Number && nil != other.Number {
@ -67,16 +59,16 @@ func (value *Value) Compare(other *Value) int {
if value.Number.Content > other.Number.Content {
return 1
} else if value.Number.Content < other.Number.Content {
return -1
} else {
return int(value.CreatedAt - other.CreatedAt)
}
if value.Number.Content < other.Number.Content {
return -1
}
return 0
} else {
if other.Number.IsNotEmpty {
return -1
}
return int(value.CreatedAt - other.CreatedAt)
return 0
}
}
case KeyTypeDate:
@ -87,37 +79,37 @@ func (value *Value) Compare(other *Value) int {
}
if value.Date.Content > other.Date.Content {
return 1
} else if value.Date.Content < other.Date.Content {
return -1
} else {
return int(value.CreatedAt - other.CreatedAt)
}
if value.Date.Content < other.Date.Content {
return -1
}
return 0
} else {
if other.Date.IsNotEmpty {
return -1
}
return int(value.CreatedAt - other.CreatedAt)
return 0
}
}
case KeyTypeCreated:
if nil != value.Created && nil != other.Created {
if value.Created.Content > other.Created.Content {
return 1
} else if value.Created.Content < other.Created.Content {
return -1
} else {
return int(value.CreatedAt - other.CreatedAt)
}
if value.Created.Content < other.Created.Content {
return -1
}
return 0
}
case KeyTypeUpdated:
if nil != value.Updated && nil != other.Updated {
if value.Updated.Content > other.Updated.Content {
return 1
} else if value.Updated.Content < other.Updated.Content {
return -1
} else {
return int(value.CreatedAt - other.CreatedAt)
}
if value.Updated.Content < other.Updated.Content {
return -1
}
return 0
}
case KeyTypeSelect, KeyTypeMSelect:
if nil != value.MSelect && nil != other.MSelect {
@ -129,35 +121,19 @@ func (value *Value) Compare(other *Value) int {
for _, v := range other.MSelect {
v2 += v.Content
}
ret := strings.Compare(v1, v2)
if 0 == ret {
ret = int(value.CreatedAt - other.CreatedAt)
}
return ret
return strings.Compare(v1, v2)
}
case KeyTypeURL:
if nil != value.URL && nil != other.URL {
ret := strings.Compare(value.URL.Content, other.URL.Content)
if 0 == ret {
ret = int(value.CreatedAt - other.CreatedAt)
}
return ret
return strings.Compare(value.URL.Content, other.URL.Content)
}
case KeyTypeEmail:
if nil != value.Email && nil != other.Email {
ret := strings.Compare(value.Email.Content, other.Email.Content)
if 0 == ret {
ret = int(value.CreatedAt - other.CreatedAt)
}
return ret
return strings.Compare(value.Email.Content, other.Email.Content)
}
case KeyTypePhone:
if nil != value.Phone && nil != other.Phone {
ret := strings.Compare(value.Phone.Content, other.Phone.Content)
if 0 == ret {
ret = int(value.CreatedAt - other.CreatedAt)
}
return ret
return strings.Compare(value.Phone.Content, other.Phone.Content)
}
case KeyTypeMAsset:
if nil != value.MAsset && nil != other.MAsset {
@ -169,11 +145,7 @@ func (value *Value) Compare(other *Value) int {
for _, v := range other.MAsset {
v2 += v.Content
}
ret := strings.Compare(v1, v2)
if 0 == ret {
ret = int(value.CreatedAt - other.CreatedAt)
}
return ret
return strings.Compare(v1, v2)
}
case KeyTypeTemplate:
if nil != value.Template && nil != other.Template {
@ -188,13 +160,9 @@ func (value *Value) Compare(other *Value) int {
if v1 < v2 {
return -1
}
return int(value.CreatedAt - other.CreatedAt)
return 0
}
ret := strings.Compare(value.Template.Content, other.Template.Content)
if 0 == ret {
ret = int(value.CreatedAt - other.CreatedAt)
}
return ret
return strings.Compare(value.Template.Content, other.Template.Content)
}
case KeyTypeCheckbox:
if nil != value.Checkbox && nil != other.Checkbox {
@ -204,7 +172,7 @@ func (value *Value) Compare(other *Value) int {
if !value.Checkbox.Checked && other.Checkbox.Checked {
return -1
}
return int(value.CreatedAt - other.CreatedAt)
return 0
}
case KeyTypeRelation:
if nil != value.Relation && nil != other.Relation {
@ -231,13 +199,9 @@ func (value *Value) Compare(other *Value) int {
if v1 < v2 {
return -1
}
return int(value.CreatedAt - other.CreatedAt)
return 0
}
ret := strings.Compare(vContent, oContent)
if 0 == ret {
ret = int(value.CreatedAt - other.CreatedAt)
}
return ret
return strings.Compare(vContent, oContent)
}
case KeyTypeRollup:
if nil != value.Rollup && nil != other.Rollup {
@ -263,14 +227,10 @@ func (value *Value) Compare(other *Value) int {
if v1 < v2 {
return -1
}
return int(value.CreatedAt - other.CreatedAt)
return 0
}
ret := strings.Compare(vContent, oContent)
if 0 == ret {
ret = int(value.CreatedAt - other.CreatedAt)
}
return ret
return strings.Compare(vContent, oContent)
}
}
return int(value.CreatedAt - other.CreatedAt)
return 0
}

View file

@ -208,6 +208,7 @@ func (table *Table) SortRows() {
})
sort.Slice(editedRows, func(i, j int) bool {
sorted := true
for _, colIndexSort := range colIndexSorts {
val1 := editedRows[i].Cells[colIndexSort.Index].Value
if nil == val1 {
@ -221,14 +222,28 @@ func (table *Table) SortRows() {
result := val1.Compare(val2)
if 0 == result {
sorted = false
continue
}
sorted = true
if colIndexSort.Order == SortOrderAsc {
return 0 > result
}
return 0 < result
}
if !sorted {
key1 := editedRows[i].GetBlockValue()
if nil == key1 {
return false
}
key2 := editedRows[j].GetBlockValue()
if nil == key2 {
return false
}
return key1.CreatedAt < key2.CreatedAt
}
return false
})