Skip to content

Commit

Permalink
♻️ Improve sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Sep 14, 2024
1 parent b6af01e commit 89f46b8
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 33 deletions.
5 changes: 2 additions & 3 deletions kernel/api/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"unicode/utf8"

"github.com/88250/gulu"
"github.com/facette/natsort"
"github.com/gin-gonic/gin"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/model"
Expand Down Expand Up @@ -252,10 +251,10 @@ func getWorkspaces(c *gin.Context) {
}
}
sort.Slice(openedWorkspaces, func(i, j int) bool {
return natsort.Compare(util.RemoveEmojiInvisible(filepath.Base(openedWorkspaces[i].Path)), util.RemoveEmojiInvisible(filepath.Base(openedWorkspaces[j].Path)))
return util.NaturalCompare(filepath.Base(openedWorkspaces[i].Path), filepath.Base(openedWorkspaces[j].Path))
})
sort.Slice(closedWorkspaces, func(i, j int) bool {
return natsort.Compare(util.RemoveEmojiInvisible(filepath.Base(closedWorkspaces[i].Path)), util.RemoveEmojiInvisible(filepath.Base(closedWorkspaces[j].Path)))
return util.NaturalCompare(filepath.Base(closedWorkspaces[i].Path), filepath.Base(closedWorkspaces[j].Path))
})
workspaces = append(workspaces, openedWorkspaces...)
workspaces = append(workspaces, closedWorkspaces...)
Expand Down
19 changes: 9 additions & 10 deletions kernel/model/backlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/88250/lute/ast"
"github.com/88250/lute/parse"
"github.com/emirpasic/gods/sets/hashset"
"github.com/facette/natsort"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/search"
"github.com/siyuan-note/siyuan/kernel/sql"
Expand Down Expand Up @@ -275,13 +274,13 @@ func GetBacklink2(id, keyword, mentionKeyword string, sortMode, mentionSortMode
case util.SortModeCreatedASC:
return backlinks[i].Created < backlinks[j].Created
case util.SortModeNameDESC:
return util.PinYinCompare(util.RemoveEmojiInvisible(backlinks[j].Name), util.RemoveEmojiInvisible(backlinks[i].Name))
return util.PinYinCompare(backlinks[j].Name, backlinks[i].Name)
case util.SortModeNameASC:
return util.PinYinCompare(util.RemoveEmojiInvisible(backlinks[i].Name), util.RemoveEmojiInvisible(backlinks[j].Name))
return util.PinYinCompare(backlinks[i].Name, backlinks[j].Name)
case util.SortModeAlphanumDESC:
return natsort.Compare(util.RemoveEmojiInvisible(backlinks[j].Name), util.RemoveEmojiInvisible(backlinks[i].Name))
return util.NaturalCompare(backlinks[j].Name, backlinks[i].Name)
case util.SortModeAlphanumASC:
return natsort.Compare(util.RemoveEmojiInvisible(backlinks[i].Name), util.RemoveEmojiInvisible(backlinks[j].Name))
return util.NaturalCompare(backlinks[i].Name, backlinks[j].Name)
}
return backlinks[i].ID > backlinks[j].ID
})
Expand All @@ -304,13 +303,13 @@ func GetBacklink2(id, keyword, mentionKeyword string, sortMode, mentionSortMode
case util.SortModeCreatedASC:
return backmentions[i].Created < backmentions[j].Created
case util.SortModeNameDESC:
return util.PinYinCompare(util.RemoveEmojiInvisible(backmentions[j].Name), util.RemoveEmojiInvisible(backmentions[i].Name))
return util.PinYinCompare(backmentions[j].Name, backmentions[i].Name)
case util.SortModeNameASC:
return util.PinYinCompare(util.RemoveEmojiInvisible(backmentions[i].Name), util.RemoveEmojiInvisible(backmentions[j].Name))
return util.PinYinCompare(backmentions[i].Name, backmentions[j].Name)
case util.SortModeAlphanumDESC:
return natsort.Compare(util.RemoveEmojiInvisible(backmentions[j].Name), util.RemoveEmojiInvisible(backmentions[i].Name))
return util.NaturalCompare(backmentions[j].Name, backmentions[i].Name)
case util.SortModeAlphanumASC:
return natsort.Compare(util.RemoveEmojiInvisible(backmentions[i].Name), util.RemoveEmojiInvisible(backmentions[j].Name))
return util.NaturalCompare(backmentions[i].Name, backmentions[j].Name)
}
return backmentions[i].ID > backmentions[j].ID
})
Expand Down Expand Up @@ -520,7 +519,7 @@ func buildLinkRefs(defRootID string, refs []*sql.Ref, keyword string) (ret []*Bl
}

var paragraphParentIDs []string
for parentID, _ := range parentRefParagraphs {
for parentID := range parentRefParagraphs {
paragraphParentIDs = append(paragraphParentIDs, parentID)
}
sqlParagraphParents := sql.GetBlocks(paragraphParentIDs)
Expand Down
13 changes: 6 additions & 7 deletions kernel/model/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/88250/lute/html"
"github.com/88250/lute/lex"
"github.com/88250/lute/parse"
"github.com/facette/natsort"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/cache"
Expand Down Expand Up @@ -150,30 +149,30 @@ func ListNotebooks() (ret []*Box, err error) {
switch Conf.FileTree.Sort {
case util.SortModeNameASC:
sort.Slice(ret, func(i, j int) bool {
return util.PinYinCompare(util.RemoveEmojiInvisible(ret[i].Name), util.RemoveEmojiInvisible(ret[j].Name))
return util.PinYinCompare(ret[i].Name, ret[j].Name)
})
case util.SortModeNameDESC:
sort.Slice(ret, func(i, j int) bool {
return util.PinYinCompare(util.RemoveEmojiInvisible(ret[j].Name), util.RemoveEmojiInvisible(ret[i].Name))
return util.PinYinCompare(ret[j].Name, ret[i].Name)
})
case util.SortModeUpdatedASC:
case util.SortModeUpdatedDESC:
case util.SortModeAlphanumASC:
sort.Slice(ret, func(i, j int) bool {
return natsort.Compare(util.RemoveEmojiInvisible(ret[i].Name), util.RemoveEmojiInvisible(ret[j].Name))
return util.NaturalCompare(ret[i].Name, ret[j].Name)
})
case util.SortModeAlphanumDESC:
sort.Slice(ret, func(i, j int) bool {
return natsort.Compare(util.RemoveEmojiInvisible(ret[j].Name), util.RemoveEmojiInvisible(ret[i].Name))
return util.NaturalCompare(ret[j].Name, ret[i].Name)
})
case util.SortModeCustom:
sort.Slice(ret, func(i, j int) bool { return ret[i].Sort < ret[j].Sort })
case util.SortModeRefCountASC:
case util.SortModeRefCountDESC:
case util.SortModeCreatedASC:
sort.Slice(ret, func(i, j int) bool { return natsort.Compare(ret[j].ID, ret[i].ID) })
sort.Slice(ret, func(i, j int) bool { return util.NaturalCompare(ret[j].ID, ret[i].ID) })
case util.SortModeCreatedDESC:
sort.Slice(ret, func(i, j int) bool { return natsort.Compare(ret[j].ID, ret[i].ID) })
sort.Slice(ret, func(i, j int) bool { return util.NaturalCompare(ret[j].ID, ret[i].ID) })
}
return
}
Expand Down
9 changes: 4 additions & 5 deletions kernel/model/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"github.com/88250/lute/html"
"github.com/88250/lute/parse"
util2 "github.com/88250/lute/util"
"github.com/facette/natsort"
jsoniter "github.com/json-iterator/go"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
Expand Down Expand Up @@ -388,23 +387,23 @@ func ListDocTree(boxID, listPath string, sortMode int, flashcard, showHidden boo
switch sortMode {
case util.SortModeNameASC:
sort.Slice(docs, func(i, j int) bool {
return util.PinYinCompare(util.RemoveEmojiInvisible(docs[i].Name), util.RemoveEmojiInvisible(docs[j].Name))
return util.PinYinCompare(docs[i].Name, docs[j].Name)
})
case util.SortModeNameDESC:
sort.Slice(docs, func(i, j int) bool {
return util.PinYinCompare(util.RemoveEmojiInvisible(docs[j].Name), util.RemoveEmojiInvisible(docs[i].Name))
return util.PinYinCompare(docs[j].Name, docs[i].Name)
})
case util.SortModeUpdatedASC:
sort.Slice(docs, func(i, j int) bool { return docs[i].Mtime < docs[j].Mtime })
case util.SortModeUpdatedDESC:
sort.Slice(docs, func(i, j int) bool { return docs[i].Mtime > docs[j].Mtime })
case util.SortModeAlphanumASC:
sort.Slice(docs, func(i, j int) bool {
return natsort.Compare(util.RemoveEmojiInvisible(docs[i].Name), util.RemoveEmojiInvisible(docs[j].Name))
return util.NaturalCompare(docs[i].Name, docs[j].Name)
})
case util.SortModeAlphanumDESC:
sort.Slice(docs, func(i, j int) bool {
return natsort.Compare(util.RemoveEmojiInvisible(docs[j].Name), util.RemoveEmojiInvisible(docs[i].Name))
return util.NaturalCompare(docs[j].Name, docs[i].Name)
})
case util.SortModeCustom:
fileTreeFiles := docs
Expand Down
15 changes: 7 additions & 8 deletions kernel/model/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"github.com/88250/lute/ast"
"github.com/emirpasic/gods/sets/hashset"
"github.com/facette/natsort"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/search"
"github.com/siyuan-note/siyuan/kernel/sql"
Expand Down Expand Up @@ -216,7 +215,7 @@ func BuildTags() (ret *Tags) {
ret = &Tags{}
labels := labelTags()
tags := Tags{}
for label, _ := range labels {
for label := range labels {
tags = buildTags(tags, strings.Split(label, "/"), 0)
}
appendTagChildren(&tags, labels)
Expand Down Expand Up @@ -248,27 +247,27 @@ func sortTags(tags Tags) {
switch Conf.Tag.Sort {
case util.SortModeNameASC:
sort.Slice(tags, func(i, j int) bool {
return util.PinYinCompare(util.RemoveEmojiInvisible(tags[i].Name), util.RemoveEmojiInvisible(tags[j].Name))
return util.PinYinCompare(tags[i].Name, tags[j].Name)
})
case util.SortModeNameDESC:
sort.Slice(tags, func(j, i int) bool {
return util.PinYinCompare(util.RemoveEmojiInvisible(tags[i].Name), util.RemoveEmojiInvisible(tags[j].Name))
return util.PinYinCompare(tags[i].Name, tags[j].Name)
})
case util.SortModeAlphanumASC:
sort.Slice(tags, func(i, j int) bool {
return natsort.Compare(util.RemoveEmojiInvisible((tags)[i].Name), util.RemoveEmojiInvisible((tags)[j].Name))
return util.NaturalCompare((tags)[i].Name, (tags)[j].Name)
})
case util.SortModeAlphanumDESC:
sort.Slice(tags, func(i, j int) bool {
return natsort.Compare(util.RemoveEmojiInvisible((tags)[j].Name), util.RemoveEmojiInvisible((tags)[i].Name))
return util.NaturalCompare((tags)[j].Name, (tags)[i].Name)
})
case util.SortModeRefCountASC:
sort.Slice(tags, func(i, j int) bool { return (tags)[i].Count < (tags)[j].Count })
case util.SortModeRefCountDESC:
sort.Slice(tags, func(i, j int) bool { return (tags)[i].Count > (tags)[j].Count })
default:
sort.Slice(tags, func(i, j int) bool {
return natsort.Compare(util.RemoveEmojiInvisible((tags)[i].Name), util.RemoveEmojiInvisible((tags)[j].Name))
return util.NaturalCompare((tags)[i].Name, (tags)[j].Name)
})
}
}
Expand All @@ -278,7 +277,7 @@ func SearchTags(keyword string) (ret []string) {
defer logging.Recover() // 定位 无法添加题头图标签 https://github.com/siyuan-note/siyuan/issues/6756

labels := labelBlocksByKeyword(keyword)
for label, _ := range labels {
for label := range labels {
_, t := search.MarkText(label, keyword, 1024, Conf.Search.CaseSensitive)
ret = append(ret, t)
}
Expand Down
10 changes: 10 additions & 0 deletions kernel/util/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,21 @@ import (
"io"
"strings"

"github.com/facette/natsort"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)

func NaturalCompare(str1, str2 string) bool {
str1 = RemoveEmojiInvisible(str1)
str2 = RemoveEmojiInvisible(str2)
return natsort.Compare(str1, str2)
}

func PinYinCompare(str1, str2 string) bool {
str1 = RemoveEmojiInvisible(str1)
str2 = RemoveEmojiInvisible(str2)

// Doc tree, backlinks, tags and templates ignores case when sorting alphabetically by name https://github.com/siyuan-note/siyuan/issues/8360
str1 = strings.ToLower(str1)
str2 = strings.ToLower(str2)
Expand Down

0 comments on commit 89f46b8

Please sign in to comment.