Skip to content

Commit fa48ef6

Browse files
committed
pkg/model: Fix title sorting.
1 parent 39cfe69 commit fa48ef6

File tree

3 files changed

+44
-57
lines changed

3 files changed

+44
-57
lines changed

pkg/model/book.go

+5-21
Original file line numberDiff line numberDiff line change
@@ -78,26 +78,10 @@ func ValidateMark(m *Mark) error {
7878
return nil
7979
}
8080

81-
// ByTitle is a type that implements sort.Interface for []*Book based on the Title field.
82-
type ByTitle []*Book
83-
84-
// Len is the number of elements in the collection.
85-
func (b ByTitle) Len() int {
86-
return len(b)
87-
}
88-
89-
// Less reports whether the element at index i should sort before the element at index j.
90-
func (b ByTitle) Less(i, j int) bool {
91-
// Compare titles case-insensitively
92-
return strings.ToLower(b[i].Title) < strings.ToLower(b[j].Title)
93-
}
94-
95-
// Swap swaps the elements at indexes i and j.
96-
func (b ByTitle) Swap(i, j int) {
97-
b[i], b[j] = b[j], b[i]
98-
}
99-
100-
// SortBooksByTitle sorts a slice of books by title.
10181
func SortBooksByTitle(books []*Book) {
102-
sort.Sort(ByTitle(books))
82+
sort.Slice(books, func(i, j int) bool {
83+
titleA := strings.ToLower(books[i].Title)
84+
titleB := strings.ToLower(books[j].Title)
85+
return titleA < titleB
86+
})
10387
}

pkg/parser/kindlemyclippings/parser.go

+3
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ func (p *KindleMyClippingsParser) Parse(inputPath string) ([]*model.Book, error)
159159
// Final summary
160160
fmt.Fprintln(os.Stderr) // Add a newline after the progress output
161161
fmt.Fprintf(os.Stderr, "Finished processing: %d lines parsed, %d books deduplicated, %d marks processed\n", lineCount, totalBooks, entryCount)
162+
163+
model.SortBooksByTitle(books)
164+
162165
return books, nil
163166
}
164167

tests/my_clippings_output.json

+36-36
Original file line numberDiff line numberDiff line change
@@ -55,42 +55,6 @@
5555
}
5656
]
5757
},
58-
{
59-
"title": "红楼梦(人文社权威定本彩皮版;国务院文化组批准,红研所校注;豆瓣读书TOP250首位,评论上万条;出版四十年,三次修订)",
60-
"author": "曹雪芹",
61-
"marks": [
62-
{
63-
"type": "HIGHLIGHT",
64-
"title": "红楼梦(人文社权威定本彩皮版;国务院文化组批准,红研所校注;豆瓣读书TOP250首位,评论上万条;出版四十年,三次修订)",
65-
"author": "曹雪芹",
66-
"location": {
67-
"location": 15868
68-
},
69-
"data": "都是尤二姐素习所穿的,不禁又伤心哭了起来。自己用个包袱一齐包了,也不命小厮丫鬟来拿,便自己提着来烧。",
70-
"createdAt": 1642265828
71-
},
72-
{
73-
"type": "HIGHLIGHT",
74-
"title": "红楼梦(人文社权威定本彩皮版;国务院文化组批准,红研所校注;豆瓣读书TOP250首位,评论上万条;出版四十年,三次修订)",
75-
"author": "曹雪芹",
76-
"location": {
77-
"location": 15869
78-
},
79-
"data": "平儿又是伤心,又是好笑,忙将二百两一包的碎银子偷了出来,",
80-
"createdAt": 1642265863
81-
},
82-
{
83-
"type": "HIGHLIGHT",
84-
"title": "红楼梦(人文社权威定本彩皮版;国务院文化组批准,红研所校注;豆瓣读书TOP250首位,评论上万条;出版四十年,三次修订)",
85-
"author": "曹雪芹",
86-
"location": {
87-
"location": 15871
88-
},
89-
"data": "又将一条裙子递与平儿,说:“这是他家常穿的,你好生替我收着,作个念心儿。”",
90-
"createdAt": 1642265884
91-
}
92-
]
93-
},
9458
{
9559
"title": "小行星掉在下午(首届宝珀理想国文学奖决选作者沈大成全新小说集,给沈大成6分钟,她给你一场颅内反乌托邦式冒险 理想国出品)",
9660
"author": "沈大成",
@@ -129,5 +93,41 @@
12993
"createdAt": 1642641563
13094
}
13195
]
96+
},
97+
{
98+
"title": "红楼梦(人文社权威定本彩皮版;国务院文化组批准,红研所校注;豆瓣读书TOP250首位,评论上万条;出版四十年,三次修订)",
99+
"author": "曹雪芹",
100+
"marks": [
101+
{
102+
"type": "HIGHLIGHT",
103+
"title": "红楼梦(人文社权威定本彩皮版;国务院文化组批准,红研所校注;豆瓣读书TOP250首位,评论上万条;出版四十年,三次修订)",
104+
"author": "曹雪芹",
105+
"location": {
106+
"location": 15868
107+
},
108+
"data": "都是尤二姐素习所穿的,不禁又伤心哭了起来。自己用个包袱一齐包了,也不命小厮丫鬟来拿,便自己提着来烧。",
109+
"createdAt": 1642265828
110+
},
111+
{
112+
"type": "HIGHLIGHT",
113+
"title": "红楼梦(人文社权威定本彩皮版;国务院文化组批准,红研所校注;豆瓣读书TOP250首位,评论上万条;出版四十年,三次修订)",
114+
"author": "曹雪芹",
115+
"location": {
116+
"location": 15869
117+
},
118+
"data": "平儿又是伤心,又是好笑,忙将二百两一包的碎银子偷了出来,",
119+
"createdAt": 1642265863
120+
},
121+
{
122+
"type": "HIGHLIGHT",
123+
"title": "红楼梦(人文社权威定本彩皮版;国务院文化组批准,红研所校注;豆瓣读书TOP250首位,评论上万条;出版四十年,三次修订)",
124+
"author": "曹雪芹",
125+
"location": {
126+
"location": 15871
127+
},
128+
"data": "又将一条裙子递与平儿,说:“这是他家常穿的,你好生替我收着,作个念心儿。”",
129+
"createdAt": 1642265884
130+
}
131+
]
132132
}
133133
]

0 commit comments

Comments
 (0)