title: Java 8 stream - sort by max duplicates descending
body: <p>Using Java 8 stream,
  I am trying to sort a list by a part of the field (groupname) maximum duplicates descending. </p>
  
  <p>It only needs to use the first part before : the second part is not relevant. And the unique row records order is not relevant. 
  (I am only trying to bring the duplicate records to top according to max duplicates.)</p>
  
  <p><strong>Test Data:</strong></p>
  
  <pre><code>100 : 200
  200 : 207
  200 : 203
  200 : 201
  300 : 202
  103 : 201
  103 : 202
  </code></pre>
  
  <p><strong>Expected:</strong></p>
  
  <pre><code>200 : 207
  200 : 203
  200 : 201
  103 : 201
  103 : 202
  100 : 200
  300 : 202
  </code></pre>
  
  <p>I tried the below code, it returns the orders correctly. But only the grouped data instead of the original full records and sorted.</p>
  
  <pre><code>200=3
  103=2
  100=1
  300=1
  </code></pre>
  
  <p><strong>Java code</strong></p>
  
  <pre><code>@Test
  public void testSplit2Optimsation() {
  
      List&lt;CompatibilityRule&gt; rules = new ArrayList&lt;&gt;();
      CompatibilityRule compatibilityRule1 = new CompatibilityRule();
      compatibilityRule1.setGroupname("100 : 200");
  
      CompatibilityRule compatibilityRule2 = new CompatibilityRule();
      compatibilityRule2.setGroupname("200 : 207");
  
      CompatibilityRule compatibilityRule3 = new CompatibilityRule();
      compatibilityRule3.setGroupname("200 : 203");
  
      CompatibilityRule compatibilityRule4 = new CompatibilityRule();
      compatibilityRule4.setGroupname("200 : 201");
  
      CompatibilityRule compatibilityRule5 = new CompatibilityRule();
      compatibilityRule5.setGroupname("300 : 202");
  
      CompatibilityRule compatibilityRule6 = new CompatibilityRule();
      compatibilityRule6.setGroupname("102 : 202");
  
      CompatibilityRule compatibilityRule7 = new CompatibilityRule();
      compatibilityRule7.setGroupname("103 : 202");
  
      rules.add(compatibilityRule1);
      rules.add(compatibilityRule2);
      rules.add(compatibilityRule3);
      rules.add(compatibilityRule4);
      rules.add(compatibilityRule5);
      rules.add(compatibilityRule6);
      rules.add(compatibilityRule7);
  
  
  
      rules.stream()
              .map(r -&gt; r.getGroupname().split(":")[0].trim())
              .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
              .entrySet().stream()
              .sorted(Map.Entry.&lt;String, Long&gt;comparingByValue().reversed())
              .forEach(System.out::println);
  
  }
  </code></pre>