title: Scala Ordered by multiple fields
body: <p>This question is probably quite dumb, but I can't find an example and can't figure it out.</p>
  
  <p>I want to compare two <code>Person</code> classes by last, first, and middle name, in that order. Here's the brain-dead way to do it:</p>
  
  <pre><code>def compare(that: Person): Int = {
    val last: Int = lastName.compare(that.lastName)
    if (last != 0) last
    else {
      val first: Int = firstName.compare(that.firstName)
      if (first != 0) first
      else middleName.compare(that.middleName)
  }
  </code></pre>
  
  <p>I know there's some much more clever way to do this (probably using <code>Ordering</code>) but I can't put my finger on it.</p>
  
  <p>Todd</p>
  
  <p>I figured out this once I realized how to access the right things in Ordering.</p>
  
  <pre><code>def compare(that: Person): Int = {
    Ordering.Tuple3(Ordering.String, Ordering.String, Ordering.String).compare(
       (lastName, firstName, middleName),
       (that.lastName, that.firstName, that.middleName))
  }
  </code></pre>
  
  <p>I'm pretty sure I can get away with fewer explicits, but this works and is reasonably compact.</p>