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.34 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.33 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.32 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.31 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.30 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.29 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.28 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.27 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.26 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.25 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.24 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.23 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.22 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.21 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.20 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.19 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.18 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.17 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.16 lib/roqua/core_ext/array/stable_sort_by.rb
roqua-support-0.1.15 lib/roqua/core_ext/array/stable_sort_by.rb