Feature #1084 [ruby-core:21709]

request for: Array#sort_by!

Added by Radosław Bułat 336 days ago. Updated 335 days ago.

Status :Closed Start :02/01/2009
Priority :Normal Due date :
Assigned to :Yukihiro Matsumoto % Done :

100%

Category :-
Target version :-

Description

$ ruby1.9.1 -e "p Array.instance_methods.grep /sort/"
[:sort, :sort!, :sort_by]

sort and sort! override it's version from Enumerable and sort_by is directly from Enumerable, but for symmetry and consistency it would be nice to have destructive sort_by! for Array.

For example:
a = [1,2,3]
a.sort_by! {|e| -e }
p a #=> [3, 2, 1]

Associated revisions

Revision 21967
Added by matz 335 days ago

History

02/02/2009 11:56 AM - Yukihiro Matsumoto

Hi,

In message "Re: [ruby-core:21709] [Feature #1084] request for: Array#sort_by!"
    on Sun, 1 Feb 2009 11:11:26 +0900, Radosław Bułat <redmine@ruby-lang.org> writes:

|sort and sort! override it's version from Enumerable and sort_by is directly from Enumerable, but for symmetry and consistency it would be nice to have destructive sort_by! for Array.

Despite how you expect, sort_by! is not faster than sort_by at all.
In fact, it would use sort_by internal then replace the receiver.  Do
you still want the "dangerous" version?

							matz.

02/02/2009 12:00 PM - Koichi Sasada

  • Assigned to set to Yukihiro Matsumoto

02/02/2009 02:51 PM - Yukihiro Matsumoto

Hi,

In message "Re: [ruby-core:21729] Re: [Feature #1084] request for: Array#sort_by!"
    on Mon, 2 Feb 2009 12:59:10 +0900, James Gray <james@grayproductions.net> writes:

|> Despite how you expect, sort_by! is not faster than sort_by at all.
|> In fact, it would use sort_by internal then replace the receiver.  Do
|> you still want the "dangerous" version?
|
|I do.  :)  I know I've typed this line a million times:
|
|   a = a.sort_by { … }

OK, OK.  I will add (sometime in the future).

							matz.

02/02/2009 04:13 PM - Kornelius Kalnbach

Yukihiro Matsumoto wrote:
> Despite how you expect, sort_by! is not faster than sort_by at all.
> In fact, it would use sort_by internal then replace the receiver.
I don't expect bang methods to be generally faster, so this wouldn't
come as a surprise. Missing sort_by! has bitten me a lot of times.

[murphy]


02/02/2009 07:47 PM - Yukihiro Matsumoto

  • Status changed from Open to Closed
  • % Done changed from 0 to 100
Applied in changeset r21967.

02/02/2009 08:29 PM - Eero Saynatkari

Excerpts from Henri Suur-Inkeroinen's message of Mon Feb 02 12:46:52 +0200 2009:
> Issue #1084 has been updated by Yukihiro Matsumoto.
> 
> Status changed from Open to Closed
> % Done changed from 0 to 100
> 
> Applied in changeset r21967.

This seems to break expectations for Enumerable, since the
method cannot be reasonably supported there (and #sort_by
is still defined in Enumerable, as I understand it.) If it
is to remain, I think this should be specifically noted in
the documentation. Additionally, static as the Enumerable
inclusion may be, definining a method that depends on a
mixin seems a bit awkward: perhaps Array should define its
own #sort_by?


Eero

--
Magic is insufficiently advanced technology.

02/02/2009 08:33 PM - Kornelius Kalnbach

Eero Saynatkari wrote:
> This seems to break expectations for Enumerable, since the
> method cannot be reasonably supported there (and #sort_by
> is still defined in Enumerable, as I understand it.) If it
> is to remain, I think this should be specifically noted in
> the documentation. Additionally, static as the Enumerable
> inclusion may be, definining a method that depends on a
> mixin seems a bit awkward: perhaps Array should define its
> own #sort_by?
matz only added Array#sort_by!.

[murphy]

02/02/2009 08:47 PM - Kornelius Kalnbach

Now the documentation seems a bit confusing:

  enum.sort_by! {| obj | block }    -> array

Shouldn't it be "array" instead of "enum"?

02/02/2009 08:57 PM - Eero Saynatkari

Excerpts from Kornelius Kalnbach's message of Mon Feb 02 13:32:33 +0200 2009:
> Eero Saynatkari wrote:
> > This seems to break expectations for Enumerable, since the
> > method cannot be reasonably supported there (and #sort_by
> > is still defined in Enumerable, as I understand it.) If it
> > is to remain, I think this should be specifically noted in
> > the documentation. Additionally, static as the Enumerable
> > inclusion may be, definining a method that depends on a
> > mixin seems a bit awkward: perhaps Array should define its
> > own #sort_by?
>
> matz only added Array#sort_by!.

Exactly why I am asking :)


Eero

--
Magic is insufficiently advanced technology.

02/03/2009 01:00 AM - Radosław Bułat

On Mon, Feb 2, 2009 at 3:30 PM, Florian Frank <flori@nixe.ping.de> wrote:
> I already asked in ruby-core:21318 and didn't get a reply, but what about
> Hash#sort and Hash#sort_by or even the !-methods? With insertion-ordered hashes
> returning hashes instead of arrays from these methods would now be semantically
> useful.
>

On the one hand it would be nice to have these methods, on the other
hand it would mean that Hash isn't anymore just simple, plain Hash
(because the can be sorted and who knows what else will in the
future). My pragmatic approach tells me that it's not as bad idea to
have it. Without it if someone want to change order of keys he can do
this by:

h = {"b" => "bar", "a" => "foo"}
h = Hash[*h.sort.flatten]
p h



-- 
Pozdrawiam

Radosław Bułat
http://radarek.jogger.pl - mój blog

02/06/2009 12:25 AM - Yukihiro Matsumoto

Hi,

In message "Re: [ruby-core:21752] Re: [Feature #1084](Closed) request for: Array#sort_by!"
    on Mon, 2 Feb 2009 23:30:47 +0900, "Florian Frank" <flori@nixe.ping.de> writes:

|I already asked in ruby-core:21318 and didn't get a reply, but what about
|Hash#sort and Hash#sort_by or even the !-methods? With insertion-ordered hashes
|returning hashes instead of arrays from these methods would now be semantically
|useful.

Although I now recognize the ordered hash as 1.9 spec, I still
hesitate to add sorting method that returns "sorted" hash to the Hash
class.

							matz.

02/06/2009 02:53 AM - Kornelius Kalnbach

Yukihiro Matsumoto wrote:
> Although I now recognize the ordered hash as 1.9 spec, I still 
> hesitate to add sorting method that returns "sorted" hash to the Hash
>  class.
I think I see your point. I always disliked Hash methods returning
arrays, but maybe Hash#select returning a Hash (new in 1.9) is enough
for now.

[murphy]

Also available in: Atom PDF