From: Greg.mpls@... Date: 2016-08-13T18:36:23+00:00 Subject: [ruby-core:76861] [Ruby trunk Feature#12648] `Enumerable#sort_by` with descending option Issue #12648 has been updated by Greg L. Yukihiro Matsumoto wrote: > I think we are talking about two things at once. > > First, adding reverse (or descending) option to `sort_by`. > I think it may be useful for some cases, but it's only slightly better than 'sort_by().reverse`. I would concur. > Second, adding secondary key sort order to `sort_by`. Just to clarify, I'm not sure what you mean by 'adding secondary key sort order', key word being 'secondary'. Original poster (Tsuyoshi Sawada) stated 'When there are multiple sort keys', so (hopefully) sort key limit of more than two... > It may be useful too for some cases, but it should be a separate method. > Do you have any name suggestion? Maybe something like ``` sort_keys([sort order values]) { |x| [f(x), g(x), ...] } ``` where `[sort order values]` is an array of composed of `1` or `-1` values. Most people would associate `1` with ascending and `-1` with descending. I think `true` and `false` don't convey an order as well. As to 'rules' for whether the two arrays must be identical in length, or if not, what occurs, that's a lot of options. At a minimum, I think `[sort order values]` should be optional, in which case all keys would sort ascending. Otherwise, it might be easiest to raise an error unless both are the same length. Sorry for the delay. ---------------------------------------- Feature #12648: `Enumerable#sort_by` with descending option https://bugs.ruby-lang.org/issues/12648#change-60086 * 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: