title: Sorting in Elasticsearch Painless
body: <p>I had the following line in a groovy script that I'm attempting to rewrite into painless -</p>
  
  <pre><code>def buckets = ctx.payload.aggregations.metrics.buckets.sort(a,b -&gt; a.largest_surprise.value == b.largest_surprise.value ? 0 : a.largest_surprise.value &lt; b.largest_surprise.value ? -1 : 1);
  </code></pre>
  
  <p>The compile error I'm getting is "Variable [a] is not defined."</p>
  
  <p>It looks like painless supports the Collections.sort method in java so I tried -</p>
  
  <pre><code>Collections.sort(ctx.payload.aggregations.metrics.buckets, a,b -&gt; a.largest_surprise.value == b.largest_surprise.value ? 0 : a.largest_surprise.value &lt; b.largest_surprise.value ? -1 : 1);
  </code></pre>
  
  <p>But that also has a compile error - "Unknown call [sort] with [3] arguments on type [Collections]."</p>
  
  <p>Is it possible to sort in painless with a lambda? If so, then what is the syntax? Also, are there any better references for painless than <a href="https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-api-reference.html" rel="nofollow noreferrer">https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-api-reference.html</a>?</p>
  
  <p>Thanks,
  Nick</p>