title: While sorting a collection or any data set, How does reverse ordering works in Java 8?
body: <p>I am learning Java 8 from examples/questions on stackoverflow while solving some algorithm questions. I have query: When using Comparator.reverseOrder() or reversed(), whether JVM understands that I want reverse order hence it applies logic in such a way that while sorting itself the order is maintained in descending fashion . Or one more pass (O(n)) is taken by JVM to change a ascending ordered datastructure to descending order datastructure.</p>
  
  <pre><code>int[][] arr= new int[][]{{1,4},{3,5}};
  Arrays.sort(arr, Comparator.&lt;int[]&gt;comparingInt(a -&gt; a[0]).reversed());
  </code></pre>
  
  <p>Another example for a Map, I perform some operations on the map trackW which i haven't copied here.</p>
  
  <pre><code>Map&lt;String,Integer&gt; hmsort= new LinkedHashMap&lt;&gt;();
  Map&lt;String,Integer&gt; trackW = new HashMap&lt;&gt;();
  
  trackW.entrySet()
              .stream()
              .sorted(Map.Entry.&lt;String,Integer&gt;comparingByValue().
                      thenComparing(Map.Entry.comparingByKey(Comparator.reverseOrder())))
              .forEachOrdered(x-&gt;hmsort.put(x.getKey(),x.getValue()));
  </code></pre>
  
  <p>Also If someone can fix/suggest better option for sorting hashmap in this way (ascending based on value and then descending based on key) I would be really glad.</p>
  
  <p>Regards.</p>