title: Sorting a List of Objects Based on 1 of its Fields
body: <p>Hello stackoverflow community!
  I am new to these forums and also fairly new to java and android programming--which happen to be the objects of my question--so sorry in advance for any blunders!</p>
  
  <p>My issue is sorting. I am looking for a method to sort objects based on a field that I choose (not sorting based on the first field, then the next, etc. exemplified by comparator chaining). I believe I've found the solution to my problem:</p>
  
  <p><a href="https://stackoverflow.com/a/5113108/1549672">https://stackoverflow.com/a/5113108/1549672</a></p>
  
  <p>but I am having trouble actually getting this to work. I have a suspicion that I'm probably missing something due to my lack of java experience, so any help is welcome!</p>
  
  <p>Here is what I am trying:</p>
  
  <p>As my class-</p>
  
  <pre><code>public class ItemLocation {
  String title;
  int id;
  }
  </code></pre>
  
  <p>As my function-</p>
  
  <pre><code>public void sort(final String field, List&lt;ItemLocation&gt; itemLocationList) {
      Collections.sort(itemLocationList, new Comparator&lt;ItemLocation&gt;() {
          @Override
          public int compare(ItemLocation o1, ItemLocation o2) {
              if(field.equals("title")) {
                  return o1.title.compareTo(o2.title);
              } else if(field.equals("id")) {
                  return Integer.valueOf(o1.id).compareTo(o2.id);
              }
              return 0;
          }
      });
  }
  </code></pre>
  
  <p>using these, could someone possibly give an example of using this method? I attempted to fill an ArrayList and sort it, but to no avail.</p>
  
  <p>Thanks for the help!</p>