Skip to content

Commit dde8886

Browse files
committed
use slices.SortFunc to sort slices of pages
1 parent 175355c commit dde8886

File tree

5 files changed

+39
-25
lines changed

5 files changed

+39
-25
lines changed

extensions/activitypub/activitypub.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
"fmt"
66
"html/template"
77
"net/url"
8-
"sort"
8+
"slices"
99
"strconv"
10+
"strings"
1011
"time"
1112

1213
. "github.com/emad-elsaid/xlog"
@@ -222,14 +223,21 @@ func outboxPage(r Request) Output {
222223
pageIndex, _ := strconv.ParseInt(r.PathValue("page"), 10, 64)
223224
pageIndex--
224225

225-
var pages orderedPages = Pages(r.Context())
226+
pages := Pages(r.Context())
226227

227228
if int(pageIndex) >= len(pages) || pageIndex < 0 {
228229
return NotFound("page index is out of context")
229230
}
230231

231232
var page Page
232-
sort.Sort(pages)
233+
slices.SortFunc(pages, func(a, b Page) int {
234+
if modtime := b.ModTime().Compare(a.ModTime()); modtime != 0 {
235+
return modtime
236+
}
237+
238+
return strings.Compare(a.Name(), b.Name())
239+
})
240+
233241
page = pages[pageIndex]
234242

235243
var u url.URL
@@ -266,9 +274,3 @@ func outboxPage(r Request) Output {
266274
},
267275
)
268276
}
269-
270-
type orderedPages []Page
271-
272-
func (a orderedPages) Len() int { return len(a) }
273-
func (a orderedPages) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
274-
func (a orderedPages) Less(i, j int) bool { return a[i].ModTime().After(a[j].ModTime()) }

extensions/hashtags/hashtags.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,15 @@ func relatedPages(p Page) template.HTML {
225225
func hashtagPages(hashtag Markdown) template.HTML {
226226
hashtag_value := strings.Trim(string(hashtag), "# \n")
227227
pages := tagPages(context.Background(), hashtag_value)
228+
228229
slices.SortFunc(pages, func(a, b Page) int {
229-
return int(b.ModTime().Unix() - a.ModTime().Unix())
230+
if modtime := b.ModTime().Compare(a.ModTime()); modtime != 0 {
231+
return modtime
232+
}
233+
234+
return strings.Compare(a.Name(), b.Name())
230235
})
236+
231237
output := Partial("hashtag-pages", Locals{"pages": pages})
232238
return template.HTML(output)
233239
}

extensions/photos/photos.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"os"
1616
"path"
1717
"path/filepath"
18-
"sort"
18+
"slices"
1919
"strings"
2020
"time"
2121

@@ -133,8 +133,8 @@ func photosShortcode(input xlog.Markdown) template.HTML {
133133
return template.HTML(err.Error())
134134
}
135135

136-
sort.Slice(photos, func(i, j int) bool {
137-
return photos[i].Time.After(photos[j].Time)
136+
slices.SortFunc(photos, func(i, j *Photo) int {
137+
return j.Time.Compare(i.Time)
138138
})
139139

140140
return xlog.Partial("photos", xlog.Locals{

extensions/recent/recent.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package recent
33
import (
44
"embed"
55
"html/template"
6-
"sort"
6+
"slices"
7+
"strings"
78

89
_ "embed"
910

@@ -28,21 +29,21 @@ func (Recent) Init() {
2829
}
2930

3031
func recentHandler(r Request) Output {
31-
var rp recentPages = Pages(r.Context())
32-
sort.Sort(rp)
32+
rp := Pages(r.Context())
33+
slices.SortFunc(rp, func(a, b Page) int {
34+
if modtime := b.ModTime().Compare(a.ModTime()); modtime != 0 {
35+
return modtime
36+
}
37+
38+
return strings.Compare(a.Name(), b.Name())
39+
})
3340

3441
return Render("recent", Locals{
3542
"page": DynamicPage{NameVal: "Recent"},
3643
"pages": rp,
3744
})
3845
}
3946

40-
type recentPages []Page
41-
42-
func (a recentPages) Len() int { return len(a) }
43-
func (a recentPages) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
44-
func (a recentPages) Less(i, j int) bool { return a[i].ModTime().After(a[j].ModTime()) }
45-
4647
type links struct{}
4748

4849
func (l links) Icon() string { return "fa-solid fa-clock-rotate-left" }

extensions/rss/main.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import (
66
"fmt"
77
"html/template"
88
"net/url"
9-
"sort"
9+
"slices"
10+
"strings"
1011
"time"
1112

1213
. "github.com/emad-elsaid/xlog"
@@ -91,8 +92,12 @@ func feed(r Request) Output {
9192
}
9293

9394
pages := Pages(r.Context())
94-
sort.Slice(pages, func(i, j int) bool {
95-
return pages[i].ModTime().After(pages[j].ModTime())
95+
slices.SortFunc(pages, func(a, b Page) int {
96+
if modtime := b.ModTime().Compare(a.ModTime()); modtime != 0 {
97+
return modtime
98+
}
99+
100+
return strings.Compare(a.Name(), b.Name())
96101
})
97102

98103
if len(pages) > limit {

0 commit comments

Comments
 (0)