Project

General

Profile

Bug #14017

Hash.sort_by inconsistent between 2.2.6 and upper versions

Added by chucke (Tiago Cardoso) about 2 years ago. Updated about 2 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
2.2.6
[ruby-core:83295]

Description

The bug occurs when sorting by a numeric value and two different keys have similar values: It is very easily demonstrated here:

enc = {"foo"=>0.9, "bar"=>1.0, "identity"=>1.0}
enc.sort_by { |_, q| -q }

# in ruby 2.2.6
#=> [["identity", 1.0], ["bar", 1.0], ["foo", 0.9]]
# in rubies > 2.2
#=> [["bar", 1.0], ["identity", 1.0], ["foo", 0.9]]

For the record, newer rubies present the correct version IMO, as the order of equivalent values shouldn't be changed based on the same value.

I looked at the ruby 2.3 release notes, but couldn't find the mention to a fix, so I can't say that this is a "behaviour change, upgrade to new" case. What I can say is that this behaviour seems to have changed in ruby 2.3 (tested also with ruby 2.2.2 and ruby 2.1.9 and can reproduce the same result as with 2.2.6).

As an example of production code which is probably returning a wrong result is rack, which uses as variation of this to select the best content encoding .

Is this something which can be backported into a possible 2.2.7, or is ruby 2.2 in maintenance mode?

History

Updated by jeremyevans0 (Jeremy Evans) about 2 years ago

  • Status changed from Open to Rejected

Ruby 2.2 is in the security maintenance phase, see https://bugs.ruby-lang.org/projects/ruby/wiki/ReleaseEngineering

As this is not a security issue, it is not suitable for backporting to ruby 2.2.

Updated by nobu (Nobuyoshi Nakada) about 2 years ago

chucke (Tiago Cardoso) wrote:

The bug occurs when sorting by a numeric value and two different keys have similar values: It is very easily demonstrated here:

It is not a bug.

For the record, newer rubies present the correct version IMO, as the order of equivalent values shouldn't be changed based on the same value.

Ruby's sort method is not stable sort.

The documentation of Array#sort_by!:

The result is not guaranteed to be stable. When two keys are equal,
the order of the corresponding elements is unpredictable.

Updated by chucke (Tiago Cardoso) about 2 years ago

got it, forgot to check the doc. Thx for clearing that up!

Updated by duerst (Martin Dürst) about 2 years ago

nobu (Nobuyoshi Nakada) wrote:

Ruby's sort method is not stable sort.

The documentation of Array#sort_by!:

The result is not guaranteed to be stable. When two keys are equal,
the order of the corresponding elements is unpredictable.

Just to be cristal clear:

A sort being stable means that when values are the same, the elements stay in the original order.
This would mean that the order is predictable.
But "the order of the corresponding elements is unpredictable" is stronger, it also inplies that the order can change across versions or implementations, or (at least in theory) across invocations.

Also available in: Atom PDF