Merge pull request #1277 from OxygenCobalt/copilot/optimize-issue-sorting

Fix intelligent sorting to split on symbols instead of removing them
This commit is contained in:
Alexander Capehart
2026-01-10 18:53:48 -07:00
committed by GitHub
2 changed files with 29 additions and 9 deletions
@@ -86,10 +86,10 @@ private data class IntelligentKnownName(override val raw: String, override val s
// optimize it
val stripped =
name
// Remove excess punctuation from the string, as those usually aren't
// considered in sorting.
.replace(punctRegex, "")
.ifEmpty { name }
// Replace punctuation with spaces to create token boundaries, improving
// sorting of names like "15-9" vs "15-10".
.replace(punctRegex, " ")
.let { if (it.isBlank()) name else it }
.run {
// Strip any english articles like "the" or "an" from the start, as music
// sorting should ignore such when possible.
@@ -212,7 +212,8 @@ class NamingTest {
assertEquals("alt-J", name.raw)
assertEquals(null, name.sort)
val only = name.tokens.single()
assertEquals("altJ", only.collationKey.sourceString)
// Punctuation is replaced with spaces to create token boundaries
assertEquals("alt J", only.collationKey.sourceString)
assertEquals(Token.Type.LEXICOGRAPHIC, only.type)
}
@@ -242,7 +243,8 @@ class NamingTest {
assertEquals("& Yet & Yet", name.raw)
assertEquals(null, name.sort)
val first = name.tokens[0]
assertEquals("Yet Yet", first.collationKey.sourceString)
// Punctuation is replaced with spaces, so each & becomes a space
assertEquals("Yet Yet", first.collationKey.sourceString)
assertEquals(Token.Type.LEXICOGRAPHIC, first.type)
}
@@ -258,7 +260,8 @@ class NamingTest {
assertEquals("2", second.collationKey.sourceString)
assertEquals(Token.Type.NUMERIC, second.type)
val third = name.tokens[2]
assertEquals(" ", third.collationKey.sourceString)
// Punctuation is replaced with spaces, so : becomes a space
assertEquals(" ", third.collationKey.sourceString)
assertEquals(Token.Type.LEXICOGRAPHIC, third.type)
val fourth = name.tokens[3]
assertEquals("3", fourth.collationKey.sourceString)
@@ -274,13 +277,15 @@ class NamingTest {
assertEquals("2", first.collationKey.sourceString)
assertEquals(Token.Type.NUMERIC, first.type)
val second = name.tokens[1]
assertEquals(" ", second.collationKey.sourceString)
// Punctuation is replaced with spaces, so + becomes a space
assertEquals(" ", second.collationKey.sourceString)
assertEquals(Token.Type.LEXICOGRAPHIC, second.type)
val third = name.tokens[2]
assertEquals("2", third.collationKey.sourceString)
assertEquals(Token.Type.NUMERIC, third.type)
val fourth = name.tokens[3]
assertEquals(" ", fourth.collationKey.sourceString)
// Punctuation is replaced with spaces, so = becomes a space
assertEquals(" ", fourth.collationKey.sourceString)
assertEquals(Token.Type.LEXICOGRAPHIC, fourth.type)
val fifth = name.tokens[4]
assertEquals("5", fifth.collationKey.sourceString)
@@ -389,6 +394,21 @@ class NamingTest {
assertEquals(-2, a.compareTo(c))
}
@Test
fun name_compareTo_intelligent_withHyphenatedNumbers() {
// This test ensures hyphenated numbers like "15-9" and "15-10" are sorted correctly.
// The hyphen should act as a token boundary, so "15-9" = [15, 9] and "15-10" = [15, 10].
val a = Naming.intelligent().name("15-9", null)
val b = Naming.intelligent().name("15-10", null)
val c = Naming.intelligent().name("16-1", null)
// 15-9 should come before 15-10 (9 < 10 in the second token)
assertEquals(-1, a.compareTo(b))
// 15-10 should come before 16-1 (15 < 16 in the first token)
assertEquals(-1, b.compareTo(c))
// 15-9 should come before 16-1 (15 < 16 in the first token)
assertEquals(-1, a.compareTo(c))
}
@Test
fun name_compareTo_intelligent_withPartialSort_withoutArticle_withoutNumeric() {
val a = Naming.simple().name("A", "C")