Consistent sorting of output
This commit is contained in:
parent
09b1f48aea
commit
82c40cc080
@ -32,7 +32,7 @@ final class PostListPageGenerator {
|
|||||||
// 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(ascending: false) { $0.startDate }
|
.sorted { $0.startDate > $1.startDate && $0.id < $1.id }
|
||||||
|
|
||||||
let totalCount = posts.count
|
let totalCount = posts.count
|
||||||
guard totalCount > 0 else {
|
guard totalCount > 0 else {
|
||||||
|
@ -2,6 +2,8 @@ import Foundation
|
|||||||
|
|
||||||
protocol DateItem {
|
protocol DateItem {
|
||||||
|
|
||||||
|
var id: String { get }
|
||||||
|
|
||||||
var startDate: Date { get }
|
var startDate: Date { get }
|
||||||
|
|
||||||
var hasEndDate: Bool { get }
|
var hasEndDate: Bool { get }
|
||||||
@ -9,6 +11,18 @@ protocol DateItem {
|
|||||||
var potentialEndDate: Date { get }
|
var potentialEndDate: Date { get }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension Sequence where Element: DateItem {
|
||||||
|
|
||||||
|
func sortedByStartDateAndId() -> [Element] {
|
||||||
|
sorted { (lhs, rhs) -> Bool in
|
||||||
|
if lhs.startDate == rhs.startDate {
|
||||||
|
return lhs.id < rhs.id
|
||||||
|
}
|
||||||
|
return lhs.startDate > rhs.startDate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
extension DateItem {
|
extension DateItem {
|
||||||
|
|
||||||
var endDate: Date? {
|
var endDate: Date? {
|
||||||
|
@ -24,8 +24,8 @@ final class LoadingContext {
|
|||||||
func results() -> LoadingResult {
|
func results() -> LoadingResult {
|
||||||
.init(
|
.init(
|
||||||
settings: settings ?? .default,
|
settings: settings ?? .default,
|
||||||
posts: posts.values.sorted(ascending: false) { $0.startDate },
|
posts: posts.values.sortedByStartDateAndId(),
|
||||||
pages: pages.values.sorted(ascending: false) { $0.startDate },
|
pages: pages.values.sortedByStartDateAndId(),
|
||||||
tags: tags.values.sorted(),
|
tags: tags.values.sorted(),
|
||||||
files: files.values.sorted { $0.id },
|
files: files.values.sorted { $0.id },
|
||||||
tagOverview: tagOverview,
|
tagOverview: tagOverview,
|
||||||
|
@ -13,14 +13,17 @@ struct AudioPlayerScript: HtmlProducer {
|
|||||||
|
|
||||||
let items: [AmplitudeSong]
|
let items: [AmplitudeSong]
|
||||||
|
|
||||||
|
private let encoder = JSONEncoder()
|
||||||
|
|
||||||
init(items: [AmplitudeSong]) {
|
init(items: [AmplitudeSong]) {
|
||||||
self.items = items
|
self.items = items
|
||||||
|
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
|
||||||
}
|
}
|
||||||
|
|
||||||
func populate(_ result: inout String) {
|
func populate(_ result: inout String) {
|
||||||
result += "<script>\nwindow.onload = () => { "
|
result += "<script>\nwindow.onload = () => { "
|
||||||
result += "Amplitude.init({ songs: "
|
result += "Amplitude.init({ songs: "
|
||||||
let songData = try! JSONEncoder().encode(items)
|
let songData = try! encoder.encode(items)
|
||||||
result += String(data: songData, encoding: .utf8)!
|
result += String(data: songData, encoding: .utf8)!
|
||||||
result += "}); };\n" // Close Amplitude.init and window.onload
|
result += "}); };\n" // Close Amplitude.init and window.onload
|
||||||
result += "function playEntry(index) { Amplitude.playSongAtIndex(index) };"
|
result += "function playEntry(index) { Amplitude.playSongAtIndex(index) };"
|
||||||
|
@ -61,7 +61,7 @@ struct PageHeader: HtmlProducer {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
var result = "<div style='display:none'>"
|
var result = "<div style='display:none'>"
|
||||||
for icon in icons {
|
for icon in icons.sorted(using: { $0.id} ) {
|
||||||
result += icon.icon.svgString
|
result += icon.icon.svgString
|
||||||
}
|
}
|
||||||
result += "</div>"
|
result += "</div>"
|
||||||
|
@ -66,7 +66,7 @@ struct PostListView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private var filteredAndSortedPosts: [Post] {
|
private var filteredAndSortedPosts: [Post] {
|
||||||
filteredPosts.sorted(ascending: false) { $0.startDate }
|
filteredPosts.sortedByStartDateAndId()
|
||||||
}
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user