https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112013-05-18T18:46:10ZRuby Issue Tracking SystemRuby master - Feature #8422: add Enumerable#reverse_sort and Enumerable#reverse_sort_byhttps://bugs.ruby-lang.org/issues/8422?journal_id=394192013-05-18T18:46:10Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li></ul><p>It can be done by <code>a.sort{|a,b| b<=>a}</code>. Do we really need to define new methods?</p>
<p>Matz.</p> Ruby master - Feature #8422: add Enumerable#reverse_sort and Enumerable#reverse_sort_byhttps://bugs.ruby-lang.org/issues/8422?journal_id=394242013-05-18T23:31:26Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>matz (Yukihiro Matsumoto) wrote:</p>
<blockquote>
<p>It can be done by <code>a.sort{|a,b| b<=>a}</code>.</p>
</blockquote>
<p>That will typically be much slower though.</p>
<p>Currently, the best way to do a reverse sort, performance-wise, is <code>a.sort.reverse!</code></p>
<p>For a random 100 number array, for example:</p>
<p><code>sort.reverse!</code> is faster than <code>sort.reverse</code> by 10% ± 1.0%<br>
<code>sort.reverse</code> is faster than <code>sort_by</code> by 6x ± 0.1<br>
<code>sort_by</code> is faster than <code>sort</code> by 60% ± 1.0%</p>
<p>Code: <a href="https://gist.github.com/marcandre/5604592" class="external">https://gist.github.com/marcandre/5604592</a></p> Ruby master - Feature #8422: add Enumerable#reverse_sort and Enumerable#reverse_sort_byhttps://bugs.ruby-lang.org/issues/8422?journal_id=394682013-05-22T02:31:06ZAnonymous
<ul></ul><p>-1, feature creep<br>
But I'm for defining <code>reverse!</code> as O(1), not really reversing anything, just treating the last element as first and first as last. (I do not know how collections are exactly implemented, so I am not sure whether this is possible.)</p> Ruby master - Feature #8422: add Enumerable#reverse_sort and Enumerable#reverse_sort_byhttps://bugs.ruby-lang.org/issues/8422?journal_id=397622013-06-07T12:02:43Zdrbrain (Eric Hodel)drbrain@segment7.net
<ul></ul><p>How do you sort an infinite Enumerable?</p>
<pre><code>ruby -rprime -e 'p Prime.each.reverse_sort.take 10'
</code></pre> Ruby master - Feature #8422: add Enumerable#reverse_sort and Enumerable#reverse_sort_byhttps://bugs.ruby-lang.org/issues/8422?journal_id=397872013-06-08T14:41:18Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>drbrain (Eric Hodel) wrote:</p>
<blockquote>
<p>How do you sort an infinite <code>Enumerable</code>?</p>
<pre><code>ruby -rprime -e 'p Prime.each.reverse_sort.take 10
</code></pre>
</blockquote>
<p>I'm not sure what your point is. How is that different from <code>p Prime.each.sort.take 10</code>? Moreover, an alternative request could be to add <code>Array#reverse_sort{_by}</code>.</p>
<p>Maybe we should consider a variation on <code>sort</code> that allows multiple criteria and independent sort directions?</p> Ruby master - Feature #8422: add Enumerable#reverse_sort and Enumerable#reverse_sort_byhttps://bugs.ruby-lang.org/issues/8422?journal_id=398652013-06-12T03:21:57Zdrbrain (Eric Hodel)drbrain@segment7.net
<ul></ul><p>Neither <code>Prime.each.reverse_sort.take 10</code> nor <code>Prime.each.sort.take 10</code> will return as there is no last value for infinite streams. You can neither reverse nor sort an infinite stream.</p>
<p>I believe adding the feature to <code>Enumerable</code> doesn't fit with its existing functionality (all beginning-of-stream oriented).</p>
<p>We need feedback from the submitter. Without it this issue should be closed.</p> Ruby master - Feature #8422: add Enumerable#reverse_sort and Enumerable#reverse_sort_byhttps://bugs.ruby-lang.org/issues/8422?journal_id=399142013-06-14T01:33:43Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>drbrain (Eric Hodel) wrote:</p>
<blockquote>
<p>Neither <code>Prime.each.reverse_sort.take 10</code> nor <code>Prime.each.sort.take 10</code> will return as there is no last value for infinite streams.</p>
</blockquote>
<p>Of course. But you are aware that <code>Enumerable</code> has a <code>sort</code> method that acts as a shortcut to <code>to_a.sort</code>?</p>
<blockquote>
<p>We need feedback from the submitter. Without it this issue should be closed.</p>
</blockquote>
<p>The relevant question here is "should there be a dedicated way to do a reverse sort". The question of where that method should be (<code>Array</code> or <code>Enumerable</code>) is accessory.</p> Ruby master - Feature #8422: add Enumerable#reverse_sort and Enumerable#reverse_sort_byhttps://bugs.ruby-lang.org/issues/8422?journal_id=399152013-06-14T03:19:15ZHanmac (Hans Mackowiak)hanmac@gmx.de
<ul></ul><p>my idea would be this, for ruby it may be cood if its converted into C++ code</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">module</span> <span class="nn">Enumerable</span>
<span class="k">def</span> <span class="nf">reverse_sort</span><span class="p">(</span><span class="o">&</span><span class="n">block</span><span class="p">)</span>
<span class="n">block</span> <span class="o">||=</span> <span class="nb">proc</span> <span class="p">{</span><span class="o">|</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="o">|</span> <span class="n">x</span> <span class="o"><=></span> <span class="n">y</span> <span class="p">}</span>
<span class="n">sort</span> <span class="p">{</span><span class="o">|</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="o">|</span> <span class="o">-</span> <span class="n">block</span><span class="p">.</span><span class="nf">call</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)}</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">reverse_sort_by</span>
<span class="k">return</span> <span class="n">to_enum</span><span class="p">(</span><span class="n">__method__</span><span class="p">)</span> <span class="k">unless</span> <span class="nb">block_given?</span>
<span class="n">sort_by</span><span class="p">{</span><span class="o">|</span><span class="n">x</span><span class="o">|</span> <span class="o">-</span> <span class="k">yield</span><span class="p">(</span><span class="n">x</span><span class="p">)}</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>