Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/unreleased/4760
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Bugfix: Fix alphabetical ordering when sorting files by name

Use locale-aware collation for file sorting by name to handle accented characters correctly.

https://github.com/owncloud/android/issues/4760
https://github.com/owncloud/android/pull/4785
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package com.owncloud.android.utils

import com.owncloud.android.domain.files.model.OCFile
import com.owncloud.android.presentation.files.SortType
import java.text.Collator
import java.util.Locale

class SortFilesUtils {
fun sortFiles(
Expand All @@ -34,12 +36,20 @@ class SortFilesUtils {
}

private fun sortByName(listOfFiles: List<OCFile>, ascending: Boolean): List<OCFile> {
val newListOfFiles =
if (ascending) listOfFiles.sortedBy { it.fileName.lowercase() }
else listOfFiles.sortedByDescending { it.fileName.lowercase() }
val collator = Collator.getInstance(Locale("en", "US")).apply {
strength = Collator.PRIMARY // Ignore accents and case
}

val comparator = compareByDescending<OCFile> { it.isFolder }
.thenComparator { a, b ->
if (ascending) {
collator.compare(a.fileName, b.fileName)
} else {
collator.compare(b.fileName, a.fileName)
}
}

// Show first the folders when sorting by name
return newListOfFiles.sortedByDescending { it.isFolder }
return listOfFiles.sortedWith(comparator)
}

private fun sortBySize(listOfFiles: List<OCFile>, ascending: Boolean): List<OCFile> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package com.owncloud.android.domain.files.usecases

import com.owncloud.android.domain.BaseUseCase
import com.owncloud.android.domain.files.model.OCFileWithSyncInfo
import java.text.Collator
import java.util.Locale

class SortFilesWithSyncInfoUseCase : BaseUseCase<List<OCFileWithSyncInfo>, SortFilesWithSyncInfoUseCase.Params>() {

Expand All @@ -33,12 +35,20 @@ class SortFilesWithSyncInfoUseCase : BaseUseCase<List<OCFileWithSyncInfo>, SortF
}

private fun sortByName(listOfFiles: List<OCFileWithSyncInfo>, ascending: Boolean): List<OCFileWithSyncInfo> {
val newListOfFiles =
if (ascending) listOfFiles.sortedBy { it.file.fileName.lowercase() }
else listOfFiles.sortedByDescending { it.file.fileName.lowercase() }
val collator = Collator.getInstance(Locale("en", "US")).apply {
strength = Collator.PRIMARY // Ignore accents and case
}

val comparator = compareByDescending<OCFileWithSyncInfo> { it.file.isFolder }
.thenComparator { a, b ->
if (ascending) {
collator.compare(a.file.fileName, b.file.fileName)
} else {
collator.compare(b.file.fileName, a.file.fileName)
}
}

// Show first the folders when sorting by name
return newListOfFiles.sortedByDescending { it.file.isFolder }
return listOfFiles.sortedWith(comparator)
}

private fun sortBySize(listOfFiles: List<OCFileWithSyncInfo>, ascending: Boolean): List<OCFileWithSyncInfo> =
Expand Down
Loading