If I have a method that adds two arrays of hashes together and then sorts them based on a specified order, is it possible to sort it again based on a second specified order?
If I my method looks something like:
def all_pets
#set order by fur color
order = ['Black', 'Orange', 'Golden', 'Mixed']
all_pets = dogs + cats #two arrays being added together
all_pets.sort_by {|r| order.index(r[:fur_color])}
end
My output currently looks like:
[{:name=>"Kitty", :fur_color=>"Black"} #cat,
{:name=>"Fido", :fur_color=>"Black"} #dog,
{:name=>"Whiskers", :fur_color=>"Black"} #cat,
{:name=>"Buttons", :fur_color=>"Orange"} #cat,
{:name=>"Mr.Cat", :fur_color=>"Golden"} #cat,
{:name=>"Sparky", :fur_color=>"Golden"} #dog,
{:name=>"Max", :fur_color=>"Mixed"} #dog]
This is great so far, I have the animals sorted by fur color exactly how I want, is it possible to then sort so cats will always come after dogs?
The two arrays being added have the exact same attributes (name and fur color), so I'm not sure how to order them by fur color first, and animal type second.
Since both arrays have the same attributes, and no animal type attribute, would it be possible to do something that puts all hashes that came from the array cat
to come after the hashes from array dog
after they've been sorted by fur color?
So my goal output would be:
[{:name=>"Fido", :fur_color=>"Black"} #dog,
{:name=>"Kitty", :fur_color=>"Black"} #cat,
{:name=>"Whiskers", :fur_color=>"Black"} #cat,
{:name=>"Buttons", :fur_color=>"Orange"} #cat,
{:name=>"Sparky", :fur_color=>"Golden"} #dog,
{:name=>"Mr.Cat", :fur_color=>"Golden"} #cat,
{:name=>"Max", :fur_color=>"Mixed"} #dog]
Thanks!