Guest User

Untitled

a guest
Apr 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. ## Chapter 10 ##
  2.  
  3. ##Sorting
  4.  
  5. #!/usr/bin/ruby
  6.  
  7. #This was Paul Idea... I am afraid of doing this kind of stuff, and check... ruby arry[].sort is not using it...
  8. class String
  9. include Comparable
  10. alias_method :old_compare, :"<=>"
  11. def <=>(other)
  12. self.downcase.old_compare(other.downcase)
  13. end
  14.  
  15. end
  16.  
  17. def array_sort arr
  18. recursive_sort arr, []
  19. end
  20.  
  21. def recursive_sort unsorted, sorted
  22. return sorted if unsorted.length < 1
  23.  
  24. to_sort = []
  25. word = unsorted.shift
  26.  
  27. unsorted.each do |element|
  28. if element < word
  29. to_sort.push word
  30. word = element
  31. else
  32. to_sort.push element
  33. end
  34. end
  35.  
  36. sorted.push word
  37.  
  38. recursive_sort to_sort, sorted
  39. end
  40.  
  41.  
  42. test =[
  43. "Sinaloa","Chihuahua","Nuevo León","Tamaulipas",
  44. "Sonora","Nayarit","baja California", "Baja California Sur",
  45. "Coahuila","Zacatecas","Durango","Guanajuato","Jalisco",
  46. "Veracruz","San Luis Potosí", "a","aa", "aaab", "ab"
  47. ]
  48.  
  49. puts "===== my sort ====="
  50. puts array_sort test
  51. puts
  52. puts "==== sort method ===="
  53. puts test.sort
  54.  
  55.  
  56. ### Shuffle
  57.  
  58.  
  59.  
  60.  
  61. ### Dictionary sort
  62.  
  63. ### Expanded english_number
  64.  
  65. ### Wedding number
  66.  
  67. ### Ninety-nine Bottles of Beer on the Wall
Add Comment
Please, Sign In to add comment