[Android] Fix the filename "natural" sort when file names have prepending 0
Nicolas Pomepuy
git at videolan.org
Wed Sep 21 11:55:25 UTC 2022
vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Wed Sep 21 07:38:18 2022 +0200| [8ece1989eed9fafde6465382895c6d3cac361a31] | committer: Duncan McNamara
Fix the filename "natural" sort when file names have prepending 0
> https://code.videolan.org/videolan/vlc-android/commit/8ece1989eed9fafde6465382895c6d3cac361a31
---
.../vlc-android/src/org/videolan/vlc/util/Kextensions.kt | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt b/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
index 6f08794aa..605150f8d 100644
--- a/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
+++ b/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
@@ -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 at sanitizeStringForAlphaCompare) {
+ if (c.isDigit() && c.digitToInt() == 0) numberOfPrependingZeros++ else break
+ }
+ for (i in 0 until (nbOfDigits - numberOfPrependingZeros - (getStartingNumber()?.numberOfDigits() ?: 0))) {
append("0")
}
append(this at sanitizeStringForAlphaCompare)
@@ -419,9 +423,10 @@ fun String.getStartingNumber(): Int? {
return try {
buildString {
for (c in this at 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
More information about the Android
mailing list