Sha256: e2533f68fa271e5155415c94fbba06c6b043faed5d1cce3a29677afc1e05d49d

Contents?: true

Size: 1.07 KB

Versions: 37

Compression:

Stored size: 1.07 KB

Contents

class Array
  # Method for stably sorting elements in an array on multiple attributes.
  #
  # * Pass the method a block with two arrays containing the attributes for which the
  #   elements should be subsequently sorted. The first attribute is applied last.
  #   If for some attribute the sort order should be reversed, the parameters x and y can
  #   be exchanged between the arrays.
  #
  # ==== Example
  #   my_array.stable_sort_by{|x, y| [
  #                            x.attribute1,
  #                            y.attribute2,
  #                            y.attribute3,
  #                            y.attribute4
  #                          ] <=> [
  #                            y.attribute1,
  #                            x.attribute2,
  #                            x.attribute3,
  #                            x.attribute4
  #                          ]}
  #
  def stable_sort_by
    sort do |x, y|
      if not x
        -1
      elsif not y
        1
      else
        if block_given?
          yield x, y
        else
          x <=> y
        end
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
roqua-support-0.1.14 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.13 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.12 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.11 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.10 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.9 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.8 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.7 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.6 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.5 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.4 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.3 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.2.2 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.2.1 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.2 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.1 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.0 lib/roqua/core_ext/array/stable_sort_by.rb