I ran into this example where s1 < s2 and s2 < s3 but (s1 < s3) is false:
var str1 = "あいかぎ"
var str2 = "あいかくしつ"
var str3 = "あいがみ:"
print(str1 < str2) // True
print(str2 < str3) // True
print(str1 < str3) // False (?)
Is this a bug or it is true that we cannot rely on string comparison is transitive (this breaks my sorting of string array)? I'm running Swift 3.
Update: all of these are False
print(str1 < str3) // False (?)
print(str1 == str3) // False (?)
print(str1 > str3) // False (?)
So some strings are not comparable with each other?
Update: a comment in How does the Swift string more than operator work pointed out that the source code for < operator is in https://github.com/apple/swift/blob/master/stdlib/public/core/String.swift, and the comparison is handled by _swift_stdlib_unicode_compare_utf8_utf8
in https://github.com/apple/swift/blob/master/stdlib/public/stubs/UnicodeNormalization.cpp
Update: These are true
print(str1 >= str3) // True
print(str1 <= str3) // True
Update: there is an issue with String.localizedCompare()
too. There are two strings where s1 = s2 but s2 > s1:
str1 = "bảo toàn"
str2 = "bảo tồn"
print(str1.localizedCompare(str2) == .orderedSame) // true
print(str2.localizedCompare(str1) == .orderedDescending) // true