Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- x = [[1],[1,1],[2],[2,1,1],[3]]
- module AndySortable
- def self.extended( arr )
- unless arr.respond_to?(:original_sort)
- mc = class << arr; self; end
- mc.class_eval do
- alias_method :original_sort, :<=>
- alias_method :<=>, :new_sort
- end
- end
- arr.each do |el|
- if el.is_a?(Array)
- puts "including in #{el}"
- el.extend( AndySortable )
- end
- end
- end
- def new_sort other
- puts "!!"
- if other.is_a?(Array)
- #puts "Arr"
- other.each do |el|
- el.extend(AndySortable) if el.is_a?( Array )
- end
- if self.length == other.length
- original_sort( other )
- else
- common_length = [length, other.length].min
- puts common_length
- if (self[0,common_length] <=> other[0,common_length]) == 0
- puts "#{self[0,common_length].inspect}"
- (other.length <=> length)
- else
- original_sort( other )
- end
- end
- end
- end
- end
- x= x.extend(AndySortable)
- x.sort
Add Comment
Please, Sign In to add comment