Ruby sort by multiple values?

ruby sort_by
ruby sort_by descending
ruby sort by date
ruby sort array of arrays
ruby sort by alphabetically
ruby sort array by first element
ruby sort high to low
ruby sort nested hash

I have an array of hashes:

a=[{ 'foo'=>0,'bar'=>1 },
   { 'foo'=>0,'bar'=>2 },
   ... ]

I want to sort the array first by each hash's 'foo', then by 'bar'. Google tells me this is how it's done:

a.sort_by {|h| [ h['foo'],h['bar'] ]}

But this gives me the ArgumentError "comparison of Array with Array failed". What does this mean?


a.sort { |a, b| [a['foo'], a['bar']] <=> [b['foo'], b['bar']] }

ruby sort_by multiple fields, Your sorting works correctly in MRI Ruby 1.8.7, 1.9.3, and 2.0.0: Item = Struct.new​(:name, :dir, :sort_dir) entries = [Item.new('favicon.ico', false, 1), Item.new('bin',  #sort_by handles multiple parameters by sorting the collection according to the first parameter. If there are any ties (in this case, multiple tasks due on Monday 5/4), it will fall back to the next parameter given (in our case, priority). Once you can picture it, it's a simple but rather powerful mechanism.


It probably means you're missing one of the fields 'foo' or 'bar' in one of your objects.

The comparison is coming down to something like nil <=> 2, which returns nil (instead of -1, 0 or 1) and #sort_by doesn't know how to handle nil.

Try this:

a.sort_by {|h| [ h['foo'].to_i, h['bar'].to_i ]}

How to use sort_by to sort by multiple parameters in Ruby, If you want it sorted the other way, you have two choices: Negate the operand (if possible) [2, 1, 3].sort_by{ |i| -i } will return [3, 2, 1] Reverse the array. [2, 1, 3].sort_by{ |i| i }.reverse will return [3, 2, 1] Learn to Use the Sort & Sort! Ruby Methods. The most basic form of sorting is provided by the Ruby sort method, which is defined by the Enumerable module. Let’s see an example: numbers = [5,3,2,1] numbers.sort # [1,2,3,5] numbers = [5,3,2,1] numbers.sort. # [1,2,3,5] numbers = [5,3,2,1] numbers.sort # [1,2,3,5]


What you have posted works in Ruby 1.8.7:

ruby-1.8.7-p302 > a = [{'foo'=>99,'bar'=>1},{'foo'=>0,'bar'=>2}]
 => [{"foo"=>99, "bar"=>1}, {"foo"=>0, "bar"=>2}] 

ruby-1.8.7-p302 > a.sort_by{ |h| [h['foo'],h['bar']] }
 => [{"foo"=>0, "bar"=>2}, {"foo"=>99, "bar"=>1}] 

ruby-1.8.7-p302 > a.sort_by{ |h| [h['bar'],h['foo']] }
 => [{"foo"=>99, "bar"=>1}, {"foo"=>0, "bar"=>2}] 

How to sort an array of Ruby objects by multiple class fields , Sorting a Ruby array by two or more class attributes may sound difficult, but as a friend of mine likes to say, "It's not too hard once you know how  You can learn more about arrays and see a few more examples on how to sort them in Learn How to Use Ruby Arrays in Less Than 10 Minutes. The Ruby sorting operator (<=>) Also called the spaceship operator, takes two parameters and returns one of three values. 0 if the two parameters are equal.-1 if the first parameter is less than the second parameter.


This exception occurs when the result array used for the comparison contains both nil and non-nil values.

How to Sort Arrays & Hashes in Ruby (Examples Included), You'll learn the different ways of sorting an array, starting with the sort method, then taking a look at sort_by for advanced sorting (by multiple values) & more. people.values.sort # => [18, 23, 54] This gives us the values but you might want to have both the values and their associated keys in a particular order. Enumerable To The Rescue! Luckily, Hash has the Enumerable module mixed in which provides us with methods like sort and sort_by. Let's give them a go:


comparison of Array with Array failed

This means (at least in my case) that the types of array elements are different. When I made sure all array items are of the same time (Integer, for example), sorting started to work.

Sort a Ruby array with multiple criteria, If you want to sort a Ruby array with a primary, secondary, etc. criterium, use a sort_by that array values (and how to replace 1 value with multiple values). This operator compares two Ruby objects and returns -1 if the object on the left is smaller, 0 if the objects are the same, and 1 if the object on the left is bigger. 1 <=> 2 # -1 2 <=> 2 # 0 2 <=> 1 # 1 Ruby’s sort method accepts a block that must return -1, 0, or 1, which it then uses to sort the values in the array.


Ruby's Sort and Sort_by, The Ruby sort method works by comparing elements of a collection through the the spaceship operator, takes two parameters and returns one of three values. First, note the block argument to the sort method. Second, note the modulo divisions done on the block parameters, and the reuse of the spaceship operator. If one is a multiple of 3, the modulo will be 0, otherwise, it will be 1 or 2. Since 0 will sort before 1 or 2, only the modulo matters here.


Sorting arrays and hashes in Ruby - Lucya Koroleva, Ruby has two handy methods that can be used for sorting arrays .sort sorts data using a set of keys generated by mapping the values of the  Obviously, the first 3 values are calculated the same as the next 3 values, just from a different set of matches. And to find which teams are tied, group_by comes in handy. Once the arrays have been built, all you need is a sort_by and you're done. To do all this, I propose the following structure


Sort_by: multiple fields with reverse sort - Ruby, boolean value. Frankly, i am not sure. i just tried it on irb. My issue is that i do not know how many columns a user will sort on, and sort_by() public Sorts enum using a set of keys generated by mapping the values in enum through the given block. The result is not guaranteed to be stable. When two keys are equal, the order of the corresponding elements is unpredictable.