Sha256: 7517b6aa5f6f9bb42803c34a013953248e2a4a859af37669930bf45d5ccef741
Contents?: true
Size: 1.62 KB
Versions: 3
Compression:
Stored size: 1.62 KB
Contents
# frozen_string_literal: true require_relative '../spec_helper' describe Enumerable do context "#stable_sort_by" do it "preserve the original order if all sort the same" do list_to_sort = [:b, :d, :c, :a, :e, :f] expect(list_to_sort.stable_sort_by { |c| 0 }).to eq(list_to_sort) end it "order by keys first and then position" do list_to_sort = [:b, :d, :c, :a, :e, :f] order = [:a, :b, :c] result = list_to_sort.stable_sort_by { |c| order.index(c) || order.length } expect(result).to eq([:a, :b, :c, :d, :e, :f]) end it "order by keys only if needed" do list_to_sort = [:b, :d, :c, :a, :e, :f] result = list_to_sort.stable_sort_by { |c| c.to_s } expect(result).to eq([:a, :b, :c, :d, :e, :f]) end end context "stable_sort" do it "preserve the original order if all sort the same" do list_to_sort = [:b, :d, :c, :a, :e, :f] expect(list_to_sort.stable_sort { |first, second| 0 }).to eq(list_to_sort) end it "order by keys first and then position" do list_to_sort = [:b, :d, :c, :a, :e, :f] order = [:a, :b, :c] result = list_to_sort.stable_sort do |first, second| first_pos = order.index(first) || order.length second_pos = order.index(second) || order.length first_pos <=> second_pos end expect(result).to eq([:a, :b, :c, :d, :e, :f]) end it "order by keys only if needed" do list_to_sort = [:b, :d, :c, :a, :e, :f] result = list_to_sort.stable_sort{ |first, second| first.to_s <=> second.to_s } expect(result).to eq([:a, :b, :c, :d, :e, :f]) end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
invoca-utils-0.6.0 | spec/unit/stable_sort_spec.rb |
invoca-utils-0.5.1 | spec/unit/stable_sort_spec.rb |
invoca-utils-0.5.0 | spec/unit/stable_sort_spec.rb |