File tree Expand file tree Collapse file tree 5 files changed +39
-25
lines changed
Expand file tree Collapse file tree 5 files changed +39
-25
lines changed Original file line number Diff line number Diff line change 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 ()) }
Original file line number Diff line number Diff line change @@ -225,9 +225,15 @@ func relatedPages(p Page) template.HTML {
225225func 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}
Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff line change @@ -3,7 +3,8 @@ package recent
33import (
44 "embed"
55 "html/template"
6- "sort"
6+ "slices"
7+ "strings"
78
89 _ "embed"
910
@@ -28,21 +29,21 @@ func (Recent) Init() {
2829}
2930
3031func 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-
4647type links struct {}
4748
4849func (l links ) Icon () string { return "fa-solid fa-clock-rotate-left" }
Original file line number Diff line number Diff line change 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 {
You can’t perform that action at this time.
0 commit comments