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 // optimize it
val stripped = val stripped =
name name
// Remove excess punctuation from the string, as those usually aren't // Replace punctuation with spaces to create token boundaries, improving
// considered in sorting. // sorting of names like "15-9" vs "15-10".
.replace(punctRegex, "") .replace(punctRegex, " ")
.ifEmpty { name } .let { if (it.isBlank()) name else it }
.run { .run {
// Strip any english articles like "the" or "an" from the start, as music // Strip any english articles like "the" or "an" from the start, as music
// sorting should ignore such when possible. // sorting should ignore such when possible.
@@ -212,7 +212,8 @@ class NamingTest {
assertEquals("alt-J", name.raw) assertEquals("alt-J", name.raw)
assertEquals(null, name.sort) assertEquals(null, name.sort)
val only = name.tokens.single() 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) assertEquals(Token.Type.LEXICOGRAPHIC, only.type)
} }
@@ -242,6 +243,7 @@ class NamingTest {
assertEquals("& Yet & Yet", name.raw) assertEquals("& Yet & Yet", name.raw)
assertEquals(null, name.sort) assertEquals(null, name.sort)
val first = name.tokens[0] val first = name.tokens[0]
// Punctuation is replaced with spaces, so each & becomes a space
assertEquals("Yet Yet", first.collationKey.sourceString) assertEquals("Yet Yet", first.collationKey.sourceString)
assertEquals(Token.Type.LEXICOGRAPHIC, first.type) assertEquals(Token.Type.LEXICOGRAPHIC, first.type)
} }
@@ -258,6 +260,7 @@ class NamingTest {
assertEquals("2", second.collationKey.sourceString) assertEquals("2", second.collationKey.sourceString)
assertEquals(Token.Type.NUMERIC, second.type) assertEquals(Token.Type.NUMERIC, second.type)
val third = name.tokens[2] val third = name.tokens[2]
// Punctuation is replaced with spaces, so : becomes a space
assertEquals(" ", third.collationKey.sourceString) assertEquals(" ", third.collationKey.sourceString)
assertEquals(Token.Type.LEXICOGRAPHIC, third.type) assertEquals(Token.Type.LEXICOGRAPHIC, third.type)
val fourth = name.tokens[3] val fourth = name.tokens[3]
@@ -274,12 +277,14 @@ class NamingTest {
assertEquals("2", first.collationKey.sourceString) assertEquals("2", first.collationKey.sourceString)
assertEquals(Token.Type.NUMERIC, first.type) assertEquals(Token.Type.NUMERIC, first.type)
val second = name.tokens[1] val second = name.tokens[1]
// Punctuation is replaced with spaces, so + becomes a space
assertEquals(" ", second.collationKey.sourceString) assertEquals(" ", second.collationKey.sourceString)
assertEquals(Token.Type.LEXICOGRAPHIC, second.type) assertEquals(Token.Type.LEXICOGRAPHIC, second.type)
val third = name.tokens[2] val third = name.tokens[2]
assertEquals("2", third.collationKey.sourceString) assertEquals("2", third.collationKey.sourceString)
assertEquals(Token.Type.NUMERIC, third.type) assertEquals(Token.Type.NUMERIC, third.type)
val fourth = name.tokens[3] val fourth = name.tokens[3]
// Punctuation is replaced with spaces, so = becomes a space
assertEquals(" ", fourth.collationKey.sourceString) assertEquals(" ", fourth.collationKey.sourceString)
assertEquals(Token.Type.LEXICOGRAPHIC, fourth.type) assertEquals(Token.Type.LEXICOGRAPHIC, fourth.type)
val fifth = name.tokens[4] val fifth = name.tokens[4]
@@ -389,6 +394,21 @@ class NamingTest {
assertEquals(-2, a.compareTo(c)) 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 @Test
fun name_compareTo_intelligent_withPartialSort_withoutArticle_withoutNumeric() { fun name_compareTo_intelligent_withPartialSort_withoutArticle_withoutNumeric() {
val a = Naming.simple().name("A", "C") val a = Naming.simple().name("A", "C")