From 5a4c6c91c5f65d6e1983659b229549e727a4da14 Mon Sep 17 00:00:00 2001 From: Lanius Trolling Date: Fri, 26 Apr 2024 19:45:09 -0400 Subject: [PATCH] Improve article sort order (2) --- .../info/mechyrdia/lore/ArticleListing.kt | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/jvmMain/kotlin/info/mechyrdia/lore/ArticleListing.kt b/src/jvmMain/kotlin/info/mechyrdia/lore/ArticleListing.kt index f7f3dd0..04ed077 100644 --- a/src/jvmMain/kotlin/info/mechyrdia/lore/ArticleListing.kt +++ b/src/jvmMain/kotlin/info/mechyrdia/lore/ArticleListing.kt @@ -14,6 +14,10 @@ import kotlinx.html.UL import kotlinx.html.a import kotlinx.html.li import kotlinx.html.ul +import java.text.CollationKey +import java.text.Collator +import java.util.* +import kotlin.Comparator data class ArticleNode(val name: String, val title: String, val subNodes: List?) @@ -31,7 +35,20 @@ suspend fun StoragePath.toArticleNode(): ArticleNode = ArticleNode( }?.sortedAsArticles() ) -private fun List.sortedAsArticles() = sortedBy { it.title }.sortedBy { it.subNodes == null } +private val collator: Collator = Collator.getInstance(Locale.US).apply { + strength = Collator.PRIMARY + decomposition = Collator.FULL_DECOMPOSITION +} + +private val collationSorter = Comparator> { (_, a), (_, b) -> + a.compareTo(b) +} + +fun List.sortedLexically(selector: (T) -> String) = map { it to collator.getCollationKey(selector(it)) } + .sortedWith(collationSorter) + .map { (it, _) -> it } + +private fun List.sortedAsArticles() = sortedLexically { it.title }.sortedBy { it.subNodes == null } private val String.isViewable: Boolean get() = Configuration.Current.isDevMode || !(endsWith(".wip") || endsWith(".old")) -- 2.25.1