Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-530] [String] sort order varies on Darwin vs. Linux #43147

Closed
mxcl opened this issue Jan 12, 2016 · 8 comments
Closed

[SR-530] [String] sort order varies on Darwin vs. Linux #43147

mxcl opened this issue Jan 12, 2016 · 8 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@mxcl
Copy link

mxcl commented Jan 12, 2016

Previous ID SR-530
Radar None
Original Reporter @mxcl
Type Bug
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee Lance (JIRA)
Priority Medium

md5: 39f03ef4603f0daa8e6d26ebcc5f0250

Issue Description:

When sorting:

["app", "deck-of-playing-cards", "FisherYates", "PlayingCard"].sort()

I get:

  [0] = "FisherYates"
  [1] = "PlayingCard"
  [2] = "app"
  [3] = "deck-of-playing-cards"

But on Linux I get:

  [0] = "app"
  [1] = "deck-of-playing-cards"
  [2] = "FisherYates"
  [3] = "PlayingCard"

This is with the snapshot `2016-01-06`. I'm installing 2016-01-11 now.

@mxcl
Copy link
Author

mxcl commented Jan 12, 2016

Same with 2016-01-11

@glessard
Copy link
Contributor

Comments copied from SR-640, as I had missed this one:
On OSX:

$ swift
Welcome to Apple Swift version 3.0-dev (LLVM f95d47afa7, Clang f66c5bb67b, Swift b745691a38). Type :help for assistance.
  1> "t" < "Tt"
$R0: Bool = false

On Linux:

$ swift
Welcome to Swift version 2.2-dev (LLVM f90171f6b9, Clang fe39b0b18f, Swift a476c2828a). Type :help for assistance.
  1> "t" < "Tt"
$R0: Bool = true

Unfortunately, it's not clear how/whether to fix the problem, because of the number of times this is mentioned as a possibility in the source code. What is the desired behaviour? The String documentation implies the "Standard Sort Order" is expected, but NSString does not do that. The NSString behaviour is probably closer to "American English (Computer, Standard Sort Order)".

@JamieEdge
Copy link

I believe that a bug lies within the string comparison, as a comment within the comparison logic for String mentions:

    // Note: this operation should be consistent with equality comparison of
    // Character.

However, from a Linux system, the comparison is not equivalent to Character:

  1> let x: Character = "A"
x: Character = {
  _representation = small {
    small = 9223372036854775617
  }
}
  2> let y: Character = "a"
y: Character = {
  _representation = small {
    small = 9223372036854775649
  }
}
  3> x < y
$R0: Bool = true
  4> "A" < "a"
$R1: Bool = false

The issue seems to lie with the processing done with UCollator within UnicodeNormalization.cpp. It appears to not be configured for case sensitivity.

@swift-ci
Copy link
Collaborator

Comment by Pavel Ivashkov (JIRA)

This applies not only to case sensitivity, as seen on SO

"2" < "=" // true on macOS, false on Linux

@norio-nomura
Copy link
Contributor

opened #8769

@dabrahams
Copy link
Collaborator

This will mostly be fixed by the new string work, wherein String's default sort order will be implemented as a lexicographical ordering of FCC-normalized UTF16 code units.

Note that on both platforms we rely on ICU for normalization services, and normalization differences among different implementations of ICU are a real possibility, so there will never be a guarantee that two arbitrary strings sort the same on both platforms.

However, for Latin-1 strings such as those in the example, the new work will fix the problem.

@swift-ci
Copy link
Collaborator

Comment by Lance Parker (JIRA)

This will be resolved by: https://github.com/apple/swift/pull/13877

@swift-ci
Copy link
Collaborator

swift-ci commented May 2, 2018

Comment by Lance Parker (JIRA)

This has been fixed in 4.2

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

6 participants