# Re: Newbie question about nested sort

**URL:** <https://www.ruby-forum.com/t/re-newbie-question-about-nested-sort/51615>\
**Category:** Ruby\
**Created:** [January 4, 2006, 2:37pm UTC](https://www.ruby-forum.com/t/re-newbie-question-about-nested-sort/51615 "2006-01-04T14:37:25Z")\
**Posts on this page:** 2\
**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:** [January 4, 2006, 2:37pm UTC](https://www.ruby-forum.com/t/re-newbie-question-about-nested-sort/51615/1 "2006-01-04T14:37:25Z")

</div>

> > print d[0]\*d[1]  
> > end
> > 
> > […]
> 
> Generally you need to do conditional evaluation based on higher prio  
> results. Using “||” or “or” won’t help here (dunno what Perl  
> does here).
> 
> You want something like  
> […]

hmmm, I don’t think he realy _want’s_ this.

Please don’t scare our new perl friends away… 🙂  
there is a simple solution to that:

char\_freq = [[“c”, 2],[“b”, 5],[“a”, 2]]  
sorted\_freq = char\_freq.sort {|a, b| (b[1]\<=\>a[1]).nonzero? ||  
a[0]\<=\>b[0] }  
sorted\_freq.each do |d|  
print d[0]\*d[1]  
end

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:** [January 4, 2006, 2:49pm UTC](https://www.ruby-forum.com/t/re-newbie-question-about-nested-sort/51615/2 "2006-01-04T14:49:28Z")

</div>

Kroeger, Simon (ext) wrote:

> > > print d[0]\*d[1]
> 
> hmmm, I don’t think he realy _want’s_ this.
> 
> Please don’t scare our new perl friends away… 🙂

:-)))

> there is a simple solution to that:
> 
> char\_freq = [[“c”, 2],[“b”, 5],[“a”, 2]]  
> sorted\_freq = char\_freq.sort {|a, b| (b[1]\<=\>a[1]).nonzero? ||  
> a[0]\<=\>b[0] }  
> sorted\_freq.each do |d|  
> print d[0]\*d[1]  
> end

I didn’t think of using #nonzero? - this clearly resembles the perl  
solution most. Although I’d say that I’d probably prefer the sort\_by  
approach with negated values…

Kind regards

```
robert
```
