Skip to content

Instantly share code, notes, and snippets.

@norman
Last active June 16, 2023 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norman/07b79f52cf901bc1e993410a08e06399 to your computer and use it in GitHub Desktop.
Save norman/07b79f52cf901bc1e993410a08e06399 to your computer and use it in GitHub Desktop.
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