Skip to content

fix(sort): alphanumeric comparator - #17390

Open
alperozturk96 wants to merge 3 commits into
masterfrom
fix/alphanumeric-comparator
Open

fix(sort): alphanumeric comparator#17390
alperozturk96 wants to merge 3 commits into
masterfrom
fix/alphanumeric-comparator

Conversation

@alperozturk96

@alperozturk96 alperozturk96 commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
@alperozturk96

Copy link
Copy Markdown
Collaborator Author

/backport to stable-34.1.x

Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
@github-actions

Copy link
Copy Markdown

blue-Light-Screenshot test failed, but no output was generated. Maybe a preliminary stage failed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

This PR replaces the legacy natural-sort implementation with a new Kotlin-based AlphanumericComparator and expands/modernizes unit tests to validate comparator correctness and ordering across more edge cases (per linked issue).

Changes:

  • Introduces com.owncloud.android.utils.sort.AlphanumericComparator (+ ChunkKind) and updates call sites to use it.
  • Removes the previous thirdparties.daveKoeller.AlphanumComparator implementation.
  • Reworks NaturalSortTest in Kotlin and broadens coverage (expected orders + comparator contract checks).

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
app/src/test/java/com/nextcloud/client/utils/NaturalSortTest.kt Converts and expands natural-sort tests; adds locale setup and contract validations.
app/src/main/java/thirdparties/daveKoeller/AlphanumComparator.java Removes the legacy third-party comparator implementation.
app/src/main/java/com/owncloud/android/utils/sort/ChunkKind.kt Adds chunk-kind enum used by the new comparator.
app/src/main/java/com/owncloud/android/utils/sort/AlphanumericComparator.kt Adds new Kotlin comparator implementation based on chunking + collator comparisons.
app/src/main/java/com/owncloud/android/utils/FileSortOrderByName.kt Switches sorting logic to use the new comparator.
app/src/main/java/com/owncloud/android/ui/fragment/contactsbackup/BackupFragment.kt Switches backup file sorting to the new comparator.
app/src/main/java/com/owncloud/android/datamodel/OCFile.java Updates compareTo to use the new comparator’s static compare.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +52 to +55
private val collators: ThreadLocal<Collator> = ThreadLocal.withInitial { Collator.getInstance() }

private val collator: Collator
get() = collators.get() ?: Collator.getInstance()
Comment on lines +70 to +75
private fun compareChunks(ours: String, theirs: String): Int {
val kind = kindOf(ours[0])
val byKind = kind.compareTo(kindOf(theirs[0]))
if (byKind != 0) {
return byKind
}
Comment on lines +99 to +110
fun sortingNeverThrowsRegardlessOfInputOrder() {
val comparator = AlphanumericComparator<String>()
val names = largeNameListWithIgnorableCharacters()

val failures = (0 until SORT_ATTEMPTS).mapNotNull { seed ->
runCatching { names.shuffled(Random(seed)).sortedWith(comparator) }
.exceptionOrNull()
?.let { "seed $seed: ${it.message}" }
}

assertTrue("Sorting must never throw, but failed for ${failures.take(FAILURES_TO_REPORT)}", failures.isEmpty())
}
Comment on lines +1 to +6
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
@github-actions

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown

Codacy

SpotBugs

CategoryBaseNew
Bad practice3232
Correctness6464
Dodgy code206206
Experimental11
Malicious code vulnerability22
Multithreaded correctness2525
Performance4040
Security1919
Total389389

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FileSortOrderByName crash : Comparison method violates its general contract!

2 participants