Refactor sort name changes

This commit is contained in:
Chris Palmeri 2023-04-15 10:05:15 -05:00
parent b34462340a
commit d04cd4ce4f
No known key found for this signature in database
GPG key ID: D9D1B9464F43E5B1

View file

@ -355,8 +355,7 @@ class SortName(name: String, musicSettings: MusicSettings) : Comparable<SortName
init { init {
var sortName = name var sortName = name
if (musicSettings.intelligentSorting) { if (musicSettings.intelligentSorting) {
// Strip any quotes, dots, and open parentheses from the beginning sortName = sortName.replace(leadingPunctuation, "")
sortName = sortName.replace(Regex("""^['".(]+"""), "")
sortName = sortName =
sortName.run { sortName.run {
@ -368,8 +367,8 @@ class SortName(name: String, musicSettings: MusicSettings) : Comparable<SortName
} }
} }
// Zero pad all numbers to (an arbitrary) five digits for better sorting // Zero pad all numbers to nine digits for better sorting
sortName = sortName.replace(Regex("""\d+""")) { it.value.padStart(5, '0') } sortName = sortName.replace(consecutiveDigits) { it.value.padStart(9, '0') }
} }
collationKey = COLLATOR.getCollationKey(sortName) collationKey = COLLATOR.getCollationKey(sortName)
@ -379,9 +378,8 @@ class SortName(name: String, musicSettings: MusicSettings) : Comparable<SortName
// TODO: This needs to be moved elsewhere. // TODO: This needs to be moved elsewhere.
thumbString = thumbString =
collationKey?.run { collationKey?.run {
var thumbChar = sourceString.firstOrNull() val thumbChar = sourceString.firstOrNull()
if (thumbChar?.isLetter() != true) thumbChar = '#' if (thumbChar?.isLetter() == true) thumbChar.uppercase() else "#"
thumbChar.uppercase()
} }
} }
@ -395,6 +393,8 @@ class SortName(name: String, musicSettings: MusicSettings) : Comparable<SortName
private companion object { private companion object {
val COLLATOR: Collator = Collator.getInstance().apply { strength = Collator.PRIMARY } val COLLATOR: Collator = Collator.getInstance().apply { strength = Collator.PRIMARY }
val leadingPunctuation: Regex = Regex("""^\p{Punct}+""")
val consecutiveDigits: Regex = Regex("""\d+""")
} }
} }