Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Chapter 10 ##
- ##Sorting
- #!/usr/bin/ruby
- #This was Paul Idea... I am afraid of doing this kind of stuff, and check... ruby arry[].sort is not using it...
- class String
- include Comparable
- alias_method :old_compare, :"<=>"
- def <=>(other)
- self.downcase.old_compare(other.downcase)
- end
- end
- def array_sort arr
- recursive_sort arr, []
- end
- def recursive_sort unsorted, sorted
- return sorted if unsorted.length < 1
- to_sort = []
- word = unsorted.shift
- unsorted.each do |element|
- if element < word
- to_sort.push word
- word = element
- else
- to_sort.push element
- end
- end
- sorted.push word
- recursive_sort to_sort, sorted
- end
- test =[
- "Sinaloa","Chihuahua","Nuevo León","Tamaulipas",
- "Sonora","Nayarit","baja California", "Baja California Sur",
- "Coahuila","Zacatecas","Durango","Guanajuato","Jalisco",
- "Veracruz","San Luis Potosí", "a","aa", "aaab", "ab"
- ]
- puts "===== my sort ====="
- puts array_sort test
- puts
- puts "==== sort method ===="
- puts test.sort
- ### Shuffle
- ### Dictionary sort
- ### Expanded english_number
- ### Wedding number
- ### Ninety-nine Bottles of Beer on the Wall
Add Comment
Please, Sign In to add comment