title: Sorting multiple values by ascending and descending
body: <p>I'm trying to sort an array of objects based upon different attributes. Some of those attributes I would like to sort in ascending order and some in descending order. I have been able to sort by ascending or descending but have been unable to combine the two.</p>
  
  <p>Here is the simple class I am working with:</p>
  
  <pre><code>class Dog
    attr_reader :name, :gender
  
    DOGS = []
  
    def initialize(name, gender)
      @name = name
      @gender = gender
      DOGS &lt;&lt; self
    end
  
    def self.all
      DOGS
    end
  
    def self.sort_all_by_gender_then_name
      self.all.sort_by { |d| [d.gender, d.name] }
    end
  end
  </code></pre>
  
  <p>I can then instantiate some dogs to be sorted later.</p>
  
  <pre><code>@rover = Dog.new("Rover", "Male")
  @max = Dog.new("Max", "Male")
  @fluffy = Dog.new("Fluffy", "Female")
  @cocoa = Dog.new("Cocoa", "Female")
  </code></pre>
  
  <p>I can then use the sort_all_by_gender_then_name method.</p>
  
  <pre><code>Dog.sort_all_by_gender_then_name
  =&gt; [@cocoa, @fluffy, @max, @rover]
  </code></pre>
  
  <p>The array it returns includes females first, then males, all sorted by name in ascending order.</p>
  
  <p>But what if I want to have gender be descending, and then name ascending, so that it would be males first and then sorted by name ascending. In this case:</p>
  
  <pre><code>=&gt; [@max, @rover, @cocoa, @fluffy]
  </code></pre>
  
  <p>Or, if I wanted it by gender ascending, but name descending:</p>
  
  <pre><code>=&gt; [@fluffy, @cocoa, @rover, @max]
  </code></pre>
  
  <p>When sorting numerical values, you can prepend a - to make it sort in reverse. However, I have been unable to find a way to do this with strings. Any help or ideas would be appreciated. Thanks. </p>