# Re: stable sort\_by?

**URL:** <https://www.ruby-forum.com/t/re-stable-sort-by/100038>\
**Category:** Ruby\
**Created:** [December 13, 2005, 12:55pm UTC](https://www.ruby-forum.com/t/re-stable-sort-by/100038 "2005-12-13T12:55:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![KroegerS\_Simon\_Sext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@KroegerS\_Simon\_Sext](https://www.ruby-forum.com/u/KroegerS_Simon_Sext)\
**Post date:** [December 13, 2005, 12:55pm UTC](https://www.ruby-forum.com/t/re-stable-sort-by/100038/1 "2005-12-13T12:55:15Z")

</div>

> > 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

---

<div class="post-metadata">

**Author:** ![KroegerS\_Simon\_Sext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@KroegerS\_Simon\_Sext](https://www.ruby-forum.com/u/KroegerS_Simon_Sext)\
**Post date:** [December 13, 2005, 1:00pm UTC](https://www.ruby-forum.com/t/re-stable-sort-by/100038/2 "2005-12-13T13:00:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KroegerS\_Simon\_Sext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@KroegerS\_Simon\_Sext](https://www.ruby-forum.com/u/KroegerS_Simon_Sext)\
**Post date:** [December 13, 2005, 1:13pm UTC](https://www.ruby-forum.com/t/re-stable-sort-by/100038/3 "2005-12-13T13:13:20Z")

</div>

Frederick R. 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

---

<div class="post-metadata">

**Author:** ![KroegerS\_Simon\_Sext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@KroegerS\_Simon\_Sext](https://www.ruby-forum.com/u/KroegerS_Simon_Sext)\
**Post date:** [December 14, 2005, 12:43am UTC](https://www.ruby-forum.com/t/re-stable-sort-by/100038/4 "2005-12-14T00:43:29Z")

</div>

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 Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![KroegerS\_Simon\_Sext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@KroegerS\_Simon\_Sext](https://www.ruby-forum.com/u/KroegerS_Simon_Sext)\
**Post date:** [December 13, 2005, 1:36pm UTC](https://www.ruby-forum.com/t/re-stable-sort-by/100038/5 "2005-12-13T13:36:19Z")

</div>

Frederick R. 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.
