title: Sorting an ArrayList based on a class data member's data member
body: <p>So, I have a class which contains another class object as its data member. I have created an ArrayList based on this prototype. Here is the code:</p>
  
  <pre><code>    package Stack;
  
  import java.io.*;
  import java.util.*;
  
  
      class Point
     {
         int x;
         int y;
  
         Point(int x, int y)
         {
             this.x = x;
             this.y = y;
         }
     }
  
     public class MergeInterval
     {
  
         Point P;
  
         MergeInterval() {}
  
  
         public static void main(String args[])
         {
             ArrayList&lt;Point&gt; arr = new ArrayList&lt;Point&gt;();
            // Point p = new Point(6,8);
  
             arr.add(new Point(6,8));
             arr.add(new Point(1,9));
             arr.add(new Point(2,4));
             arr.add(new Point(4,7));
  
          //   System.out.println(arr.get(1).x + " " + arr.get(1).y);
  
         }
     }
  </code></pre>
  
  <p>I need to sort this Arraylist as to get the output as following:
  {1,9} {2,4} {4,7} {6,8}</p>
  
  <p>Basically I needed to sort this structure based on the 'x' variable of Class 'Point' but using the inbuilt 'sort' method. How do I achieve it?</p>