Sort on two arrays : Enumerable « Collections « Ruby






Sort on two arrays



class MultiArray
  include Enumerable

  def initialize(*arrays)
    @arrays = arrays
  end

  def each
    @arrays.each { |a| a.each { |x| yield x } }
  end
end

ma = MultiArray.new([1, 2], [3], [4])

ma.sort                                   # => [1, 2, 3, 4]
mixed_type_ma = MultiArray.new([1, 2, 3], ["a", "b", "c"])
mixed_type_ma.sort
# ArgumentError: comparison of Fixnum with String failed

 








Related examples in the same category

1.get Enumerable involved
2.Sorting an Array by Frequency of Appearance
3.Add cartesian to Enumerable
4.Building a Histogram
5.Call each a number of times
6.randomly each
7.Writing Block Methods that Classify or Collect
8.Implementing Enumerable - Write One Method, Get 22 Free
9.Enumerable.instance_methods.sort