title: How to sort a collection by multiple values AND nulls at the end using the available convenient methods of Kotlin
body: <p>I've used the <strong>sortedWith</strong> and <strong>compareBy</strong> methods before and it is capable of sorting using multiple parameters.</p>
  
  <p>I've also used <strong>nullsLast</strong> method before and it is quite convenient just like the other two.</p>
  
  <p>With these methods, however, I can't figure out a way to sort a collection with the following sorting rules:</p>
  
  <p>Given the Class:</p>
  
  <pre class="lang-java prettyprint-override"><code>data class MyClass(
      val varA: String?,
      val varB: String
  )
  </code></pre>
  
  <ul>
  <li><p>sort according to <strong>varA</strong> alphabetically and nulls/blank to be last; then</p></li>
  <li><p>sort according to <strong>varB</strong> alphabetically</p></li>
  </ul>
  
  <p>So let's say I have the following collection:</p>
  
  <pre class="lang-java prettyprint-override"><code>val collection = listOf(
      MyClass(null, "A"),
      MyClass("a", "B"),
      MyClass("c", "C"),
      MyClass("b", "D"),
      MyClass("", "E"),
      MyClass("a", "F"),
      MyClass("b", "G"),
      MyClass(null, "H"),
      MyClass("", "I")
  )
  </code></pre>
  
  <p>it should then be sorted to:</p>
  
  <pre class="lang-java prettyprint-override"><code>    MyClass("a", "B")
      MyClass("a", "F")
      MyClass("b", "D")
      MyClass("b", "G")
      MyClass("c", "C")
      MyClass(null, "A")
      MyClass("", "E")
      MyClass(null, "H")
      MyClass("", "I")
  </code></pre>
  
  <p>is there a one-liner code I can use just like the <strong>compareBy</strong> that uses <em>vararg</em> parameter</p>