Last active
June 16, 2023 12:33
-
-
Save norman/07b79f52cf901bc1e993410a08e06399 to your computer and use it in GitHub Desktop.
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
strings = %w[aa áb ac na ña ne oa] | |
# Provide alphabetical sort in Spanish, where accented vowels are sorted | |
# the same as unaccented vowels, and "ñ" is sorted directly after "n". | |
# | |
# This is accomplished by normalizing the string to Unicode decomposed form | |
# to break characters with diacritics into multiple characters, and then | |
# removing acute accents and diaresis: e.g. "á" becomes "a", "ü" becomes | |
# "u". These are the only two diacritics used for vowels in Spanish. | |
# | |
# The tilde is left on the "ñ", which allows the meals to be alphabetized | |
# correctly with "ñ" between "n" and "o". | |
def spanish_sort(array) | |
array.sort do |a, b| | |
a.unicode_normalize(:nfd).delete("\u0301\u0308") <=> b.unicode_normalize(:nfd).delete("\u0301\u0308") | |
end | |
end | |
pp strings.sort #=> ["aa", "ac", "na", "ne", "oa", "áb", "ña"] | |
pp spanish_sort(strings) #=> ["aa", "áb", "ac", "na", "ne", "ña", "oa"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment