0

I have an array of objects Dog. Dog has several fields:

  • name (String)
  • color (String)
  • kind (String)

If I want to sort the array by name, color, and kind in ascending order, I can do:

dogs.sort_by { |dog| [dog.name, dog.color, dog.kind] }

but what if I want to reverse only by the first field, i.e.,

  • name (descending)
  • color (ascending)
  • kind (ascending)

Is there any solution without introducing some hash representing color and kind by Intergers (we can apply - to integers when sorting)?

sawa
  • 165,429
  • 45
  • 277
  • 381
Deck
  • 1,969
  • 4
  • 20
  • 41

0 Answers0