Sha256: d3527c5396185691784f66020be123599a9d7f72ada7300a090db6bf02a36fdf

Contents?: true

Size: 1.09 KB

Versions: 37

Compression:

Stored size: 1.09 KB

Contents

require 'roqua/core_ext/array/stable_sort_by'

describe Array do
  describe "#stable_sort_by" do
    it "wraps #sort" do
      array = []
      array.should_receive(:sort)
      array.stable_sort_by
    end

    it "sorts nil values before all others" do
      [1, nil, 3].stable_sort_by.should == [nil, 1, 3]
    end

    it "defaults to regular comparison" do
      [1, 3, 2].stable_sort_by.should == [1, 2, 3]
    end

    it "accepts a block to do complex comparison" do
      [{a: 2, b: 2, c: 3},
       {a: 2, b: 2, c: 4},
       {a: 1, b: 1, c: 6}].stable_sort_by do |x, y|
        [x[:a], x[:b], x[:c]] <=> [y[:a], y[:b], y[:c]]
       end.should == [{a: 1, b: 1, c: 6},
                      {a: 2, b: 2, c: 3},
                      {a: 2, b: 2, c: 4}]
    end

    it "leaves items in original order if they are the same" do
      [{a: 2, b: 2, c: 4},
       {a: 2, b: 1, c: 3},
       {a: 1, b: 3, c: 6}].sort do |x, y|
        [x[:a], x[:b]] <=> [y[:a], y[:b]]
       end.should == [{a: 1, b: 3, c: 6},
                      {a: 2, b: 1, c: 3},
                      {a: 2, b: 2, c: 4}]

    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

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