Has Array#sort become stable?

Viewed 59

From the docs

"When the block returns zero, the order for a and b is indeterminate, and may be unstable:

a = 'abcde'.split('').shuffle
a # => ["e", "b", "d", "a", "c"]
a1 = a.sort {|a, b| 0 }
a1 # =>  ["c", "e", "b", "d", "a"]

"

If I run this code the sort appears to be stable:

puts RUBY_VERSION # => 3.0.3
p a = 'abcde'.split('').shuffle  # => ["d", "e", "c", "b", "a"]
p a1 = a.sort {|a, b| 0 } # => ["d", "e", "c", "b", "a"]

Has Ruby sneakily got a stable sort?

0 Answers
Related