Skip to content

Instantly share code, notes, and snippets.

@roktas
Last active August 29, 2015 14:13
Show Gist options
  • Save roktas/4c266a966eeaee3647ff to your computer and use it in GitHub Desktop.
Save roktas/4c266a966eeaee3647ff to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
module Turkish
def self.native_downcased_alphabet
'abcçdefgğhıijklmnoöprsştuüvyz'
end
def self.native_upcased_alphabet
'ABCÇDEFGĞHIIJKLMNOÖPRSŞTUÜVYZ'
end
def self.extended_downcased_alphabet
'aâbcçdeêfgğhıîijklmnoôöpqrsştuûüvwxyz'
end
def self.extended_upcased_alphabet
'AÂBCÇDEÊFGĞHIÎİJKLMNOÔÖPQRSŞTUÛÜVWXYZ'
end
def self.character_ordinal
return @character_ordinal if @character_ordinal
@character_ordinal = {}
(0..127).map(&:chr).join # in standard ascii table
.sub(/[A-Z]+/, extended_upcased_alphabet) # replace upcased letters
.sub(/[a-z]+/, extended_downcased_alphabet) # replace downcased letters
.split('').each_with_index do |c, i| # store character ordinals
@character_ordinal[c] = i # by using character indexes
end
@character_ordinal
end
refine Array do
def sort
lookup = Turkish.character_ordinal
sort_by do |item|
item.is_a?(String) ? item.chars.map { |c| lookup[c] || 0 } : super
end
end
end
end
using Turkish
# rubocop:disable Metrics/MethodLength
def test
p %w(
şö
a
â
i
ı
z
î
mc-roktas
orbit-roktas
ssh-aFeefaaa1234
systemd-private-dlDwbA
tmpFUPn84
tmpHKgaWt
tmpQPkwGD
tmux-1001
v4WIQiE
vEF1Jkp
vZYYuRW
ycm_tempBjj3IN
ycm_tempehpcPl
ycm_tempRdI7WA
).sort
end
test if __FILE__ == $PROGRAM_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment