Forum: Ruby Re: stable sort_by?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
87e9a89c53ccf984db792113471c2171?d=identicon&s=25 Kroeger, Simon (ext) (Guest)
on 2005-12-13 12:55
(Received via mailing list)
> > I have to sort a list using a number of criterions (a,b,c,d) where d
> >     "Team B"=>{:a=>0,  :b=>-2, :c=>4, :d=>112 },
> > pp tmp
> > because of criterion d. Is it possible that sort_by is not
> stable? Or
> > is there something I did wrong?
>
>
> Wouldn't this be simpler ? :
>
>  h.sort_by {|k,v| [v[:a],v[:b],v[:c],v[:d]]}.reverse
>
> Best Regards,
> Fred

Full Ack,

obviously sort_by isn't stable:

test = [[1, 'b'], [1, 'c'], [0, 'a']]
p test.sort_by{|t|t[0]}

=> [[0, "a"], [1, "c"], [1, "b"]]
(ruby 1.8.2 (2004-12-25) [i386-mswin32])

cheers

Simon
8546e5131cc66da30ccd062234a75b12?d=identicon&s=25 Frederick Ros (sleeper)
on 2005-12-13 13:00
Kroeger, Simon (ext) wrote:

> Full Ack,
>
> obviously sort_by isn't stable:
>
> test = [[1, 'b'], [1, 'c'], [0, 'a']]
> p test.sort_by{|t|t[0]}
>
> => [[0, "a"], [1, "c"], [1, "b"]]
> (ruby 1.8.2 (2004-12-25) [i386-mswin32])

Hummm not sure to understand ... You asked for a sort on the first item
... So, in this case [1,"c"] and [1,"b"] are equivalent, and their order
is un-important ...

If you do value the order of the 2nd item of the array too, you just
have to specify it :

  test = [[1, 'b'], [1, 'c'], [0, 'a']]
  p test.sort_by{|t| t }

  => [[0, "a"], [1, "b"], [1, "c"]]

Best Regards,
Fred
Af95bdaf87958c40150b813e94381bfd?d=identicon&s=25 Christer Nilsson (christer)
on 2005-12-13 13:13
Frederick Ros wrote:
>   test = [[1, 'b'], [1, 'c'], [0, 'a']]
>   p test.sort_by{|t| t }
>
>   => [[0, "a"], [1, "b"], [1, "c"]]

which is the same as
    test.sort

Christer
10d4acbfdaccb4eee687a428ca00a5d8?d=identicon&s=25 Jim Weirich (weirich)
on 2005-12-13 13:36
Frederick Ros wrote:
> Hummm not sure to understand ... You asked for a sort on the first item
> ... So, in this case [1,"c"] and [1,"b"] are equivalent, and their order
> is un-important ...

A stable sort will leave items in the original order if their keys are
equal.  Since sort_by apparently is _not_ a stable sort, the secondary
keys must be included as you suggested.
132a94ca65959bda6c74fae54bff2425?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2005-12-14 00:43
(Received via mailing list)
On Dec 13, 2005, at 3:54 AM, Kroeger, Simon (ext) wrote:

>>> Hi,
>>>
>>> end
>>>
>> Best Regards,
> (ruby 1.8.2 (2004-12-25) [i386-mswin32])
>
> cheers
>
> Simon

  class Array
    def stable_sort
      n = 0
      sort_by {|x| n+= 1; [x, n]}
    end
  end

 >> test = [[1, 'b'], [1, 'c'], [0, 'a']]
=> [[1, "b"], [1, "c"], [0, "a"]]
 >> p test.stable_sort{|t|t[0]}
[[0, "a"], [1, "b"], [1, "c"]]

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
This topic is locked and can not be replied to.