Issue #1089 has been updated by rogerdpack (Roger Pack).


It's sad that #sort on linux is "mostly stable" but on OS X is unstable [1].  It's confusing, unfortunately.
Since almost always when I do a "sort_by(&:x).sort_by(&:y)" I am looking for a stable sort.  And that's exactly the reason that java gives for defaulting to a stable sort if it's dealing with objects [2]. Might be nice to add a stable sort option, here's a fork with a timsort: https://github.com/ruby/ruby/compare/master...phluid61:timsort
benchmarks are here: https://www.ruby-forum.com/t/timsort-in-ruby/235311/7 it's slightly slower than the unstable sort.
Anyway it would be nice to at least have it around in core, and, in my mind, be the default for at least sort_by but that's up to discretion...

[1] https://github.com/crystal-lang/crystal/issues/2350#issuecomment-550188746
[2] https://stackoverflow.com/a/15154287/32453 "Stability is a big deal when sorting arbitrary objects. For example, suppose you have objects representing email messages, and you sort them first by date, then by sender. You expect them to be sorted by date within each sender, but that will only be true if the sort is stable."

----------------------------------------
Feature #1089: Stable sorting for sort and sort_by
https://bugs.ruby-lang.org/issues/1089#change-82555

* Author: murphy (Kornelius Kalnbach)
* Status: Rejected
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
=begin
 I'd like to have stable sorting in Ruby. Stable sorting means that if two items are ==, they will retain their order in the list after sorting. In other words, stable sort does never switch two equal items.
 
 In Ruby 1.9.1, you'd have to use something like
 
   enum.sort_by.with_index { |a, i| [a, i] }
 
 to achieve a stable sort in Ruby.
 
 It would also be nice to minimize the number of comparisons, since method calling is expensive in Ruby.
 
 Python is using a highly optimized implementation of a variant of merge sort, which is stable and uses very few comparisons (called Timsort after Tim Peters); maybe Ruby can adopt it for sort and sort_by:
 
   http://svn.python.org/projects/python/trunk/Objects/listsort.txt
 
 Introducing stable sorting would not be a problem for old code because the order of equal items after sorting is currently not specified.
=end




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>