title: Java: how to sort and group a list of objects by their attributes
body: <p>For Example I have a java object called RecordGroup. class signature is give below:</p>
  
  <p>public class RecordGroup {</p>
  
  <pre><code>private String owner;
  private Integer startRow;
  private Integer recordCount;
  
  public RecordGroup() {
  }
  
  public RecordGroup(String owner, Integer startRow, Integer recordCount) {
      this.owner = owner;
      this.startRow = startRow;
      this.recordCount = recordCount;
  }
  
  public String getOwner() {
      return owner;
  }
  
  public void setOwner(String owner) {
      this.owner = owner;
  }
  
  public Integer getRecordCount() {
      return recordCount;
  }
  
  public void setRecordCount(Integer recordCount) {
      this.recordCount = recordCount;
  }
  
  public Integer getStartRow() {
      return startRow;
  }
  
  public void setStartRow(Integer startRow) {
      this.startRow = startRow;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And, i have a List which holds a list of above object as given below.</p>
  
  <p>public class Test {</p>
  
  <p>List&lt;'RecordGroup'> mergerMap = new ArrayList&lt;'RecordGroup'>();</p>
  
  <pre><code>    mergerMap.add(new RecordGroup("RECORD", 1, 6));
      mergerMap.add(new RecordGroup("RECORD", 7, 9));
      mergerMap.add(new RecordGroup("RECORD", 3, 4));
      mergerMap.add(new RecordGroup("ZONE", 3, 1));
      mergerMap.add(new RecordGroup("MODULE", 5, 6));
      mergerMap.add(new RecordGroup("ZONE", 14, 28));
      mergerMap.add(new RecordGroup("ZONE", 6, 30));
      mergerMap.add(new RecordGroup("MODULE", 1, 60));
      mergerMap.add(new RecordGroup("OFFICE", 2, 4));
      mergerMap.add(new RecordGroup("OFFICE", 8, 6));
      mergerMap.add(new RecordGroup("USER", 1, 6));
      mergerMap.add(new RecordGroup("USER", 9, 8));
      mergerMap.add(new RecordGroup("USER", 5, 7));
      mergerMap.add(new RecordGroup("OFFICE", 3, 1));
  </code></pre>
  
  <p>}</p>
  
  <p>My Question is, how to sort the above List of RecordGroup objects by their 'owner' and 'startRow' so that it can group the records by owner i.e first "ZONE" group and then "OFFICE" group and then "USER" group and then "MODULE" and finally "RECORD" group should appear in the list. It should also consider the "startRow" field while sorting and grouping i.e arrange each group by value of the "startRow" field in ascending order.</p>
  
  <p>Out put should be like this:</p>
  
  <pre><code>mergerMap.add(new RecordGroup("ZONE", 3, 1));
  mergerMap.add(new RecordGroup("ZONE", 6, 30));
  mergerMap.add(new RecordGroup("ZONE", 14, 28));
  mergerMap.add(new RecordGroup("OFFICE", 2, 4));
  mergerMap.add(new RecordGroup("OFFICE", 3, 1));
  mergerMap.add(new RecordGroup("OFFICE", 8, 6));
  mergerMap.add(new RecordGroup("USER", 1, 6));
  mergerMap.add(new RecordGroup("USER", 5, 7));
  mergerMap.add(new RecordGroup("USER", 9, 8));
  mergerMap.add(new RecordGroup("MODULE", 1, 60));
  mergerMap.add(new RecordGroup("OFFICE", 2, 4));
  mergerMap.add(new RecordGroup("MODULE", 5, 6));
  mergerMap.add(new RecordGroup("RECORD", 1, 6));
  mergerMap.add(new RecordGroup("RECORD", 3, 4));
  mergerMap.add(new RecordGroup("RECORD", 7, 9));
  </code></pre>