Skip to content

Instantly share code, notes, and snippets.

@illescasDaniel
Last active June 9, 2020 12:00
Show Gist options
  • Select an option

  • Save illescasDaniel/ca9bd8913faa5ba3832835b2251a9167 to your computer and use it in GitHub Desktop.

Select an option

Save illescasDaniel/ca9bd8913faa5ba3832835b2251a9167 to your computer and use it in GitHub Desktop.
Sort strings ignoring diacritics
import Foundation
let names = [
"camión",
"camiones",
"camizn"
]
// Better version !
// ["camión", "camiones", "camizn"]
print(
names.sorted { $0.localizedCaseInsensitiveCompare($1) == .orderedAscending }
)
// ["camizn", "camiones", "camión"]
print(
names.sorted { $0.localizedCaseInsensitiveCompare($1) == .orderedDescending }
)
// other version
//
//extension Array where Element == String {
//
// public func sortedIgnoringDiacritics() -> [String] {
// self.sortedIgnoringDiacritics(by: <)
// }
//
// public func sortedIgnoringDiacritics(by areInIncreasingOrder: (String, String) -> Bool) -> [String] {
// self.sorted { lhs, rhs in
// let lhsMapped = lhs.folding(options: .diacriticInsensitive, locale: .current)
// let rhsMapped = rhs.folding(options: .diacriticInsensitive, locale: .current)
// return areInIncreasingOrder(lhsMapped, rhsMapped)
// }
// }
//}
//print(names.sortedIgnoringDiacritics())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment