title: Java Method Reference Operator
body: <p>Only posting this here because I have absolutely no idea how to google this.  Consider the following code.  Below, we use <code> people.sort(Comparator.comparingInt(Person::getAge));</code></p>
  <p>to sort through the people so that their ages are stored from least to greatest.</p>
  <p>How can we sort from greatest to least using Comparator and a method reference operator?</p>
  <pre><code>import java.util.Collections;
  import java.util.Comparator;
  import java.util.List;
  
  class Person {
      private String name;
      private Integer age;
  
      public Person(String name, Integer age) {
          this.name = name;
          this.age = age;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public Integer getAge() {
          return age;
      }
  
      public void setAge(Integer age) {
          this.age = age;
      }
  
      @Override
      public String toString() {
          return &quot;{&quot; +
                  &quot;name='&quot; + name + '\'' +
                  &quot;, age=&quot; + age +
                  '}';
      }
  }
  
  public class Main {
      public static void main(String[] args) {
          List&lt;Person&gt; people = new ArrayList&lt;&gt;();
          people.add(new Person(&quot;Sachin&quot;, 47));
          people.add(new Person(&quot;Chris&quot;, 34));
          people.add(new Person(&quot;Rajeev&quot;, 25));
          people.add(new Person(&quot;David&quot;, 31));
  
          System.out.println(&quot;Person List : &quot; + people);
  
          // Sort People by their Age
          //people.sort((person1, person2) -&gt; {
              // return person2.getAge() - person1.getAge();
          });
  
          // A more concise way of writing the above sorting function
          people.sort(Comparator.comparingInt(Person::getAge));
  
          System.out.println(&quot;Sorted Person List by Age : &quot; + people);
  
          // You can also sort using Collections.sort() method by passing the custom Comparator
          Collections.sort(people, Comparator.comparing(Person::getName));
          System.out.println(&quot;Sorted Person List by Name : &quot; + people);
      }
  }
  </code></pre>