Project

General

Profile

Feature #15725

Proposal: Add Array#reverse_sort, #revert_sort!, #reverse_sort_by, and #reverse_sort_by!

Added by sikachu (Prem Sichanugrist) about 1 month ago. Updated about 1 month ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:91957]

Description

Hello,

I would like to propose four new methods: #reverse_sort, #revert_sort!, #reverse_sort_by, and #reverse_sort_by! to Array.

These methods provides the most efficient way for user to get a reversed sorted array by calling sort(_by) then .reverse!.

As a language designer, I believe that we should suggest the most efficient way to perform a certain action to user. By introduce these methods, we can discourage user to use them instead of do array.sort.reverse or array.sort {|a,b| b<=>a} which are less performant.

I've looked through Redmine, and I saw that there was a proposal to introduce a similar method to Enumerable in https://bugs.ruby-lang.org/issues/8422, but was somewhat rejected due to the complication with infinite Enumerable. Given that Array is finite, I think this is the best place for these methods to be implemented.

I've attached a patch to implement these methods based on other ary_sort related methods. Please let me know your thought on how I could improve them to be mergeable, as I am not proficient in writing C.

I'm looking forward to hear your thoughts about this proposal. Thank you very much.


Files


Related issues

Related to Ruby trunk - Feature #12648: `Enumerable#sort_by` with descending optionOpenActions

History

#1

Updated by mame (Yusuke Endoh) about 1 month ago

  • Related to Feature #12648: `Enumerable#sort_by` with descending option added

Updated by shevegen (Robert A. Heiler) about 1 month ago

As this is a duplicate it may be best to discuss it in #12648.

I have not yet commented so just briefly; I am mostly neutral, can see
both ways. I think a slight problem is that the method names can be
a bit long and clumsy, especially #reverse_sort_by! - this is in my
opinion a bit too long.

I am also not completely sure whether we really need it. I myself usually
use .sort and .sort_by, and then apply .reverse afterwards. But I can also
understand people wanting to do all in one go; we just should see that the
method names are useful, if this is approved that is (see what matz wrote
in the other issue). But again, I have no real strong preference either
way.

Also available in: Atom PDF