Sha256: a25782b32463c11b253513555599ef3ae2b575389c1b5491afbd19ac9da143bf

Contents?: true

Size: 303 Bytes

Versions: 4

Compression:

Stored size: 303 Bytes

Contents

module Enumerable
  def stable_sort(&compare)
    compare = compare || ->(a, b){a <=> b}
    xis = self.each_with_index.map{|x, i| [x, i]}
    sorted = xis.sort do |(a, ai), (b, bi)|
      primary = compare.call(a, b)
      primary != 0 ? primary : (ai <=> bi)
    end
    sorted.map(&:first)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
abstractivator-0.0.19 lib/enumerable_ext.rb
abstractivator-0.0.18 lib/enumerable_ext.rb
abstractivator-0.0.16 lib/enumerable_ext.rb
abstractivator-0.0.15 lib/enumerable_ext.rb