Xcode Sort by Name is doing weak ordering with Unicode

Originator:adigitalknight
Number:rdar://50854433 Date Originated:May 17 2019, 12:03 AM
Status:Open Resolved:
Product:Developer Tools Product Version:
Classification:Bug Reproducible:Always
 
Summary:
The sort algorithm used by Xcode to "Sort by Name" is not a strict sorting algorithm: in some situations, two files may be ordered differently (sometimes one before the other, and sometimes the reverse order). This happens notably when comparing Unicode characters that have the same ASCII transliteration. It also means that Xcode has a broken implementation of the Unicode Collation Algorithm (on which Finder sorting is based on, according to https://support.apple.com/kb/TA22935?locale=en_US).

Steps to Reproduce:
My repro case involves having three files named respectively:
- "ffi " (with a space at the end)
- "ffi1"
- "ffi1" (this is a unicode character followed by a digit, and it needs to be in a different folder from the previous file on some filesystems)
Then we add those files one by one to the same group in an Xcode project.
And finally we use Xcode feature "Sort by Name" on the group.

To understand the setup, I've built such scenario in an xcodeproj and I've attached it to the report. I've made a repository of it at https://github.com/Coeur/XcodeWeakSortByName in case the attachment is deleted.
There are two groups with different initial order (but same files):
- tosort-case-1 has initially this order: "ffi ", "ffi1", "ffi1"
- tosort-case-2 has initially this order: "ffi1", "ffi ", "ffi1"

Expected Results:
"Sort by Name" should return a unique strict order for the files, no matter how they are previously arranged.
Which means that tosort-case-1 and tosort-case-2 should end up with the same files order after sorting.

Actual Results:
We get multiple different sort results depending on the initial position of the group entries.
From the above and attached setup, applying "Sort by Name" results in:
- tosort-case-1 has a final order of: "ffi1", "ffi ", "ffi1"
- tosort-case-2 has a final order of: "ffi ", "ffi1", "ffi1"

Version/Build:
Xcode 10.2 on macOS Mojave 10.14.5.
System ruby: ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]

Configuration:
MacBook Pro (13-inch, 2017, Four Thunderbolt 3 Ports)

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!