title: Sort List according to more than only constraint in Scala
body: <p>I am desperately trying to find a way to sort a List of strings, where the strings are predefined identifiers of following form: a1.1, a1.2,..., a1.100, a2.1, a2.2,....,a2.100,...,b1.1, b1.2,.. and so on, which is alread the correct ordering. So each identifier is first ordered by its first character (descending alphabetic order) and within this ordering descending ordered by consecutive numbers. I have tried sortWith by providing a sorting function specifying the above rule for all two consecutive list members.</p>
  
  <pre><code>scala&gt; List("a1.102", "b2.2", "b2.1", "a1.1").sortWith((a: String, b: String) =&gt; a.take(1) &lt; b.take(1) &amp;&amp; a.drop(1).toDouble &lt; b.drop(1).toDouble)
  res2: List[java.lang.String] = List(a1.102, a1.1, b2.2, b2.1)
  </code></pre>
  
  <p>This is not the ordering I expected. However, by swapping the ordering of the expressions, as</p>
  
  <pre><code>scala&gt; List("a1.102", "b2.2", "b2.1", "a1.1").sortWith((a: String, b: String) =&gt; (a.drop(1).toDouble &lt; b.drop(1).toDouble &amp;&amp; a.take(1) &lt; b.take(2)))
  res3: List[java.lang.String] = List(a1.1, a1.102, b2.1, b2.2)
  </code></pre>
  
  <p>this indeed gives me (at least for this example) the desired ordering, which I do not understand neither. </p>
  
  <p>I would be so thankful, if somebody could give me a hint what exactly is going on there and how I can sort lists as I wish (with a more complex boolean expression than only comparing &lt; or >). A further question: The strings I am sorting (in my example) are actually keys from a HashMap m. Will any solution effect sorting m by its keys within</p>
  
  <pre><code>m.toSeq.sortWith((a: (String, String), b: (String, String)) =&gt; a._1.drop(1).toDouble &lt; b._1.drop(1).toDouble &amp;&amp; a._1.take(1) &lt; b._1.take(1))
  </code></pre>
  
  <p>Many thanks in advanced!</p>