Last active
June 9, 2020 12:00
-
-
Save illescasDaniel/ca9bd8913faa5ba3832835b2251a9167 to your computer and use it in GitHub Desktop.
Sort strings ignoring diacritics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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