tags:

views:

441

answers:

4

Hi,

I have an array of hashes like following

[
  { :foo => 'foo', :bar => 2 },
  { :foo => 'foo', :bar => 3 },
  { :foo => 'foo', :bar => 5 },
]

I am trying to sort above array in descending order according to the value of :bar in each hash.

I am using sort_by like following to sort above array.

a.sort_by { |h| h[:bar] }

However above sorts the array in ascending order. How do I make it sort in descending order?

One solution was to do following:

a.sort_by { |h| -h[:bar] }

But that negative sign does not seem appropriate. Any views?

+3  A: 

Just a quick thing, that denotes the intent of descending order.

descending = -1
a.sort_by { |h| h[:bar] * descending }

(Will think of a better way in the mean time) ;)

EDIT

a.sort_by { |h| h[:bar] }.reverse!
Pablo Fernandez
Pablo, nice job on finding a better way! See the benchmark I did.
Greg
+2  A: 

What about:

 a.sort {|x,y| y[:bar]<=>x[:bar]}

edit

It works!!

irb
>> a = [
?>   { :foo => 'foo', :bar => 2 },
?>   { :foo => 'foo', :bar => 3 },
?>   { :foo => 'foo', :bar => 5 },
?> ]
=> [{:bar=>2, :foo=>"foo"}, {:bar=>3, :foo=>"foo"}, {:bar=>5, :foo=>"foo"}]

>>  a.sort {|x,y| y[:bar]<=>x[:bar]}
=> [{:bar=>5, :foo=>"foo"}, {:bar=>3, :foo=>"foo"}, {:bar=>2, :foo=>"foo"}]
OscarRyz
Yeah it actually works, but I think the PO wants to show intent with the code (he has a working solution already).
Pablo Fernandez
@Pablo Ya veo.. y que mejor manera de mostrar sus intenciones que comparando directamente un elemento con otro ;)
OscarRyz
@Oscar parece que es cuestion de gustos nomas :D
Pablo Fernandez
+5  A: 

You could do:

a.sort{|a,b| b[:bar] <=> a[:bar]}
JRL
@JRL: I'll make your day... there you go +1 .. .congratulation you're 10k user now.. now go to the tools section above and enjoy your new powers!
OscarRyz
I want those too!! :( hehe, congrats @JRL!
Pablo Fernandez
You're almost there Pablo, you just need another... 5,740 :) **edit** +1 5,730 now
OscarRyz
+1  A: 

It's always enlightening to do a benchmark on the various suggested answers. Here's what I found out:

#!/usr/bin/ruby

require 'benchmark'

ary = []
1000.times { 
  ary << {:bar => rand(1000)} 
}

n = 500
Benchmark.bm(20) do |x|
  x.report("sort")               { n.times { ary.sort{ |a,b| b[:bar] <=> a[:bar] } } }
  x.report("sort reverse")       { n.times { ary.sort{ |a,b| a[:bar] <=> b[:bar] }.reverse } }
  x.report("sort_by -a[:bar]")   { n.times { ary.sort_by{ |a| -a[:bar] } } }
  x.report("sort_by a[:bar]*-1") { n.times { ary.sort_by{ |a| a[:bar]*-1 } } }
  x.report("sort_by.reverse!")   { n.times { ary.sort_by{ |a| a[:bar] }.reverse } }
end

                          user     system      total        real
sort                  3.960000   0.010000   3.970000 (  3.990886)
sort reverse          4.040000   0.000000   4.040000 (  4.038849)
sort_by -a[:bar]      0.690000   0.000000   0.690000 (  0.692080)
sort_by a[:bar]*-1    0.700000   0.000000   0.700000 (  0.699735)
sort_by.reverse!      0.650000   0.000000   0.650000 (  0.654447)

I think it's interesting that @Pablo's sort_by{...}.reverse! is fastest. Before running the test I thought it would be slower than "-a[:bar]" but negating the value turns out to take longer than it does to reverse the entire array in one pass. It's not much of a difference, but every little speed-up helps.

Greg