Skip to content
Snippets Groups Projects
Commit 8cf43a61 authored by Henk's avatar Henk
Browse files

Removed Array#stable_sort_by, since it was not stable and the name was wrong.

parent 37310c4e
No related branches found
No related tags found
1 merge request!36Removed Array#stable_sort_by, since it was not stable and the name was wrong.
Pipeline #33272 passed
## 0.2.0
* Removed Array#stable_sort_by, since it was not stable and the name was wrong.
## 0.1.21
* Add Errors.add_parameters(add_to: 'error_report')
......
module Roqua
module Support
VERSION = '0.1.34'.freeze
VERSION = '0.2.0'.freeze
end
end
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
\ No newline at end of file
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment