mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-19 16:10:12 +01:00
🎨 Improve doc tree Name Alphabet sorting https://github.com/siyuan-note/siyuan/issues/14773
This commit is contained in:
parent
e46c998a6b
commit
b35e21587b
3 changed files with 38 additions and 4 deletions
|
|
@ -148,11 +148,11 @@ func ListNotebooks() (ret []*Box, err error) {
|
||||||
switch Conf.FileTree.Sort {
|
switch Conf.FileTree.Sort {
|
||||||
case util.SortModeNameASC:
|
case util.SortModeNameASC:
|
||||||
sort.Slice(ret, func(i, j int) bool {
|
sort.Slice(ret, func(i, j int) bool {
|
||||||
return util.PinYinCompare(ret[i].Name, ret[j].Name)
|
return util.PinYinCompare4FileTree(ret[i].Name, ret[j].Name)
|
||||||
})
|
})
|
||||||
case util.SortModeNameDESC:
|
case util.SortModeNameDESC:
|
||||||
sort.Slice(ret, func(i, j int) bool {
|
sort.Slice(ret, func(i, j int) bool {
|
||||||
return util.PinYinCompare(ret[j].Name, ret[i].Name)
|
return util.PinYinCompare4FileTree(ret[j].Name, ret[i].Name)
|
||||||
})
|
})
|
||||||
case util.SortModeAlphanumASC:
|
case util.SortModeAlphanumASC:
|
||||||
sort.Slice(ret, func(i, j int) bool {
|
sort.Slice(ret, func(i, j int) bool {
|
||||||
|
|
|
||||||
|
|
@ -372,11 +372,11 @@ func ListDocTree(boxID, listPath string, sortMode int, flashcard, showHidden boo
|
||||||
switch sortMode {
|
switch sortMode {
|
||||||
case util.SortModeNameASC:
|
case util.SortModeNameASC:
|
||||||
sort.Slice(docs, func(i, j int) bool {
|
sort.Slice(docs, func(i, j int) bool {
|
||||||
return util.PinYinCompare(docs[i].Name, docs[j].Name)
|
return util.PinYinCompare4FileTree(docs[i].Name, docs[j].Name)
|
||||||
})
|
})
|
||||||
case util.SortModeNameDESC:
|
case util.SortModeNameDESC:
|
||||||
sort.Slice(docs, func(i, j int) bool {
|
sort.Slice(docs, func(i, j int) bool {
|
||||||
return util.PinYinCompare(docs[j].Name, docs[i].Name)
|
return util.PinYinCompare4FileTree(docs[j].Name, docs[i].Name)
|
||||||
})
|
})
|
||||||
case util.SortModeUpdatedASC:
|
case util.SortModeUpdatedASC:
|
||||||
sort.Slice(docs, func(i, j int) bool { return docs[i].Mtime < docs[j].Mtime })
|
sort.Slice(docs, func(i, j int) bool { return docs[i].Mtime < docs[j].Mtime })
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,40 @@ func PinYinCompare(str1, str2 string) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PinYinCompare4FileTree(str1, str2 string) bool {
|
||||||
|
// 文档树字母排序不复用 PinYinCompare 而是单独实现
|
||||||
|
// Improve doc tree Name Alphabet sorting https://github.com/siyuan-note/siyuan/issues/14773
|
||||||
|
|
||||||
|
str1 = RemoveEmojiInvisible(str1)
|
||||||
|
str1 = strings.TrimSuffix(str1, ".sy")
|
||||||
|
str2 = RemoveEmojiInvisible(str2)
|
||||||
|
str2 = strings.TrimSuffix(str2, ".sy")
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
|
||||||
|
a, _ := UTF82GBK(str1)
|
||||||
|
b, _ := UTF82GBK(str2)
|
||||||
|
|
||||||
|
// 长度相等的情况下,直接比较字节数组
|
||||||
|
if len(a) == len(b) {
|
||||||
|
return bytes.Compare(a, b) < 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// 长度不相等的情况下,比较前面相等的部分
|
||||||
|
if len(a) < len(b) {
|
||||||
|
if 0 == bytes.Compare(a, b[:len(a)]) { // 前面相等的情况下,比较长度
|
||||||
|
return len(a) > len(b[:len(a)])
|
||||||
|
}
|
||||||
|
return bytes.Compare(a, b[:len(a)]) < 0
|
||||||
|
}
|
||||||
|
if 0 == bytes.Compare(a[:len(b)], b) {
|
||||||
|
return len(a[:len(b)]) > len(b)
|
||||||
|
}
|
||||||
|
return bytes.Compare(a[:len(b)], b) < 0
|
||||||
|
}
|
||||||
|
|
||||||
// UTF82GBK transform UTF8 rune into GBK byte array.
|
// UTF82GBK transform UTF8 rune into GBK byte array.
|
||||||
func UTF82GBK(src string) ([]byte, error) {
|
func UTF82GBK(src string) ([]byte, error) {
|
||||||
GB18030 := simplifiedchinese.All[0]
|
GB18030 := simplifiedchinese.All[0]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue