mirror of
https://github.com/Fishwaldo/huginn.git
synced 2025-03-15 19:31:26 +00:00
Make TupleSorter#sort! never fail
If comparison of two objects fails, they are compared as strings.
This commit is contained in:
parent
b735e7300f
commit
8c2b11a567
2 changed files with 20 additions and 3 deletions
|
@ -94,9 +94,8 @@ module Utils
|
|||
def <=> other
|
||||
other = other.array
|
||||
@array.each_with_index do |e, i|
|
||||
case cmp = e <=> other[i]
|
||||
when nil
|
||||
return nil
|
||||
o = other[i]
|
||||
case cmp = e <=> o || e.to_s <=> o.to_s
|
||||
when 0
|
||||
next
|
||||
else
|
||||
|
|
|
@ -153,5 +153,23 @@ describe Utils do
|
|||
Utils.sort_tuples!(tuples, orders)
|
||||
expect(tuples).to eq expected
|
||||
end
|
||||
|
||||
it "always succeeds in sorting even if it finds pairs of incomparable objects" do
|
||||
time = Time.now
|
||||
tuples = [
|
||||
[2, "a", time - 1], # 0
|
||||
[1, "b", nil], # 1
|
||||
[1, "b", time], # 2
|
||||
["2", nil, time], # 3
|
||||
[1, nil, time], # 4
|
||||
[nil, "a", time + 1], # 5
|
||||
[2, "a", time], # 6
|
||||
]
|
||||
orders = [true, false, true]
|
||||
expected = tuples.values_at(3, 6, 0, 4, 2, 1, 5)
|
||||
|
||||
Utils.sort_tuples!(tuples, orders)
|
||||
expect(tuples).to eq expected
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue