Skip to content
Snippets Groups Projects
Commit 8ece1989 authored by Nicolas Pomepuy's avatar Nicolas Pomepuy Committed by Duncan McNamara
Browse files

Fix the filename "natural" sort when file names have prepending 0

parent af3831c1
No related branches found
No related tags found
1 merge request!1481Change the browser filename sort to a more "natural" one
Pipeline #264868 passed with stage
in 4 minutes and 29 seconds
......@@ -392,7 +392,11 @@ fun <T> Flow<T>.launchWhenStarted(scope: LifecycleCoroutineScope): Job = scope.l
fun String?.sanitizeStringForAlphaCompare(nbOfDigits: Int): String? {
if (this == null) return null
if (first().isDigit()) return buildString {
for (i in 0 until (nbOfDigits - (getStartingNumber()?.numberOfDigits() ?: 0))) {
var numberOfPrependingZeros =0
for (c in this@sanitizeStringForAlphaCompare) {
if (c.isDigit() && c.digitToInt() == 0) numberOfPrependingZeros++ else break
}
for (i in 0 until (nbOfDigits - numberOfPrependingZeros - (getStartingNumber()?.numberOfDigits() ?: 0))) {
append("0")
}
append(this@sanitizeStringForAlphaCompare)
......@@ -419,9 +423,10 @@ fun String.getStartingNumber(): Int? {
return try {
buildString {
for (c in this@getStartingNumber)
if (c.isDigit())
append(c)
else break
//we exclude starting "0" to prevent bad sorts
if (c.isDigit()) {
if (!(this.isEmpty() && c.digitToInt() == 0)) append(c)
} else break
}.toInt()
} catch (e: NumberFormatException) {
null
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment