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