Guest User

Untitled

a guest
Jun 18th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. x = [[1],[1,1],[2],[2,1,1],[3]]
  2.  
  3.  
  4. module AndySortable
  5.  
  6. def self.extended( arr )
  7. unless arr.respond_to?(:original_sort)
  8. mc = class << arr; self; end
  9. mc.class_eval do
  10. alias_method :original_sort, :<=>
  11. alias_method :<=>, :new_sort
  12. end
  13. end
  14. arr.each do |el|
  15. if el.is_a?(Array)
  16. puts "including in #{el}"
  17. el.extend( AndySortable )
  18. end
  19. end
  20. end
  21.  
  22. def new_sort other
  23. puts "!!"
  24. if other.is_a?(Array)
  25. #puts "Arr"
  26. other.each do |el|
  27. el.extend(AndySortable) if el.is_a?( Array )
  28. end
  29.  
  30. if self.length == other.length
  31. original_sort( other )
  32. else
  33. common_length = [length, other.length].min
  34. puts common_length
  35. if (self[0,common_length] <=> other[0,common_length]) == 0
  36. puts "#{self[0,common_length].inspect}"
  37. (other.length <=> length)
  38. else
  39. original_sort( other )
  40. end
  41. end
  42. end
  43. end
  44. end
  45.  
  46. x= x.extend(AndySortable)
  47. x.sort
Add Comment
Please, Sign In to add comment