[ruby-core:76664] [Ruby trunk Feature#12648] `Enumerable#sort_by` with descending option
From:
nobu@...
Date:
2016-08-02 13:34:25 UTC
List:
ruby-core #76664
Issue #12648 has been updated by Nobuyoshi Nakada.
I prefer more descriptive option, e.g., `enum.sort_by(:descend) {|e| e}`.
https://github.com/ruby/ruby/compare/trunk...nobu:feature/12648-sort_by-order
----------------------------------------
Feature #12648: `Enumerable#sort_by` with descending option
https://bugs.ruby-lang.org/issues/12648#change-59884
* Author: Tsuyoshi Sawada
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
I would like to pass an optional argument to `Enumerable#sort_by` or `Enumerable#sort_by!` to allow descending sort. When the sort key is singular, this could be done by passing a single optinal boolean variable that represents ascending when `false` (default) and descending when `true`:
```ruby
[3, 1, 2].sort_by(&:itself) # => [1, 2, 3]
[3, 1, 2].sort_by(false, &:itself) # => [1, 2, 3]
[3, 1, 2].sort_by(true, &:itself) # => [3, 2, 1]
```
When there are multiple sort keys, corresponding numbers of arguments should be passed:
```ruby
[3, 1, 2, 0].sort_by{|e| [e % 2, e]} # => [0, 2, 1, 3]
[3, 1, 2, 0].sort_by(false, false){|e| [e % 2, e]} # => [0, 2, 1, 3]
[3, 1, 2, 0].sort_by(false, true){|e| [e % 2, e]} # => [2, 0, 3, 1]
[3, 1, 2, 0].sort_by(true, false){|e| [e % 2, e]} # => [1, 3, 0, 2]
[3, 1, 2, 0].sort_by(true, true){|e| [e % 2, e]} # => [3, 1, 2, 0]
```
--
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>