Correctly sort posts during generation
This commit is contained in:
parent
431a953b03
commit
a29e6229c1
@ -32,11 +32,14 @@ final class PostListPageGenerator {
|
|||||||
"\(source.pageUrlPrefix(for: language))/\(pageNumber).html"
|
"\(source.pageUrlPrefix(for: language))/\(pageNumber).html"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Create pages for the given posts, sorted by date (newest first)
|
||||||
|
*/
|
||||||
func createPages(for posts: [Post]) {
|
func createPages(for posts: [Post]) {
|
||||||
// Sort by newest first, filter drafts
|
// Sort by newest first, filter drafts
|
||||||
let posts = posts
|
let posts = posts
|
||||||
.filter { !$0.isDraft }
|
.filter { !$0.isDraft }
|
||||||
.sorted { $0.startDate > $1.startDate && $0.id < $1.id }
|
.sortedByStartDateAndId()
|
||||||
|
|
||||||
let totalCount = posts.count
|
let totalCount = posts.count
|
||||||
guard totalCount > 0 else {
|
guard totalCount > 0 else {
|
||||||
|
@ -13,6 +13,10 @@ protocol DateItem {
|
|||||||
|
|
||||||
extension Sequence where Element: DateItem {
|
extension Sequence where Element: DateItem {
|
||||||
|
|
||||||
|
/**
|
||||||
|
Sort the elements first by their start date (newer first),
|
||||||
|
and then by id, if the start date is equal.
|
||||||
|
*/
|
||||||
func sortedByStartDateAndId() -> [Element] {
|
func sortedByStartDateAndId() -> [Element] {
|
||||||
sorted { (lhs, rhs) -> Bool in
|
sorted { (lhs, rhs) -> Bool in
|
||||||
if lhs.startDate == rhs.startDate {
|
if lhs.startDate == rhs.startDate {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user