title: How to sort inner object while parsing json data with gson
body: <p>I have two classes. </p>
  
  <p>Class SubCategory</p>
  
  <pre><code>public class SubCategory implements Comparable&lt;SubCategory&gt;{
  
      private int scid;
      private String scname;
  
      public SubCategory(int scid, String scname) {
          this.scid = scid;
          this.scname = scname;
      }
  
      public int getScid() {
          return scid;
      }
  
      public void setScid(int scid) {
          this.scid = scid;
      }
  
      public String getScname() {
          return scname;
      }
  
      public void setScname(String scname) {
          this.scname = scname;
      }
  
      public int compareTo(SubCategory subCategory){
          return this.getScname().compareTo(subCategory.getScname());
      }
  
  }
  </code></pre>
  
  <p>Class Category</p>
  
  <pre><code>public class Category implements Comparable&lt;Category&gt;{
      private int id;
      private String name;
      private String image;
      private int display_with;
      private List&lt;SubCategory&gt; subcategories;
  
      public Category(int id, String name, String image, int display_with, List&lt;SubCategory&gt; subcategories) {
          this.id = id;
          this.name = name;
          this.image = image;
          this.display_with = display_with;
          this.subcategories = subcategories;
      }
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getImage() {
          return image;
      }
  
      public void setImage(String image) {
          this.image = image;
      }
  
      public int getDisplay_with() {
          return display_with;
      }
  
      public void setDisplay_with(int display_with) {
          this.display_with = display_with;
      }
  
      public List&lt;SubCategory&gt; getSubcategories() {
          return subcategories;
      }
  
      public void setSubcategories(List&lt;SubCategory&gt; subcategories) {
          this.subcategories = subcategories;
      }
  
      public int compareTo(Category category){
          return this.getName().compareTo(category.getName());
      }
  
  }    
  </code></pre>
  
  <p>This is retrofir request.    </p>
  
  <pre><code> Retrofit retrofit = new Retrofit.Builder()
                  .baseUrl(Constants.BASERESTURL)
                  .addConverterFactory(GsonConverterFactory.create())
                  .build();
  </code></pre>
  
  <p>I am Parsing the data with gson library in retrofit response</p>
  
  <pre><code>  public void onResponse(retrofit.Response&lt;List&lt;Category&gt;&gt; response, Retrofit retrofit) {
        loading.dismiss();
        List&lt;Category&gt; categories = response.body();
        Collections.sort(categories);
        Utility.setCategories(categories);
    }    
  </code></pre>
  
  <p>i can sort the categories with Collections.sort(categories);</p>
  
  <p>But how to sort subcategories with category??</p>