title: How Comparator.compare() works?
body: <p>Learning Kotlin, I'm trying to understand how Java's <a href="https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html" rel="nofollow noreferrer">Comparator</a> interface works - mainly the <code>compare()</code> function so I can utilize it. </p>
  
  <p>I've tried reading the docs for <code>compare()</code> but I'd like a much simpler explanation of how it works. </p>
  
  <p>What exactly is <code>x</code> and <code>y</code> in <code>compare(x, y)</code> when iterating over a list? Does it target &amp; compare each pair of numbers when iterating? e.g:</p>
  
  <pre><code>arrayOf(1, 2, 3, 4)
  </code></pre>
  
  <p>would it compare 1 and 2 (x and y), then 2 and 3 (x and y), then 3 and 4 (x and y)? </p>
  
  <p>I have a Kotlin function that provides a comparator to sort a list in a descending order:</p>
  
  <pre><code>import java.util.*
  
  fun getList(): List&lt;Int&gt; {
      val arrayList = arrayListOf(1, 5, 2)
      Collections.sort(arrayList, object: Comparator&lt;Int&gt; {
          override fun compare(x: Int, y: Int){
              return x &lt; y
          }
      } )
      return arrayList
  </code></pre>
  
  <p>I'm not sure why the above function isn't the right syntax to complete that.</p>