title: Sorting an arraylist of jsonobjects on a particular json element
body: <p>I have written some code that creates an array list of json data using the Gson library which looks something like this.</p>
  
  <pre><code>{
   "filename": "somefile.mp3"
   "artist": "artist1"
   "title": "title1"
   "genre": "rock"
  }
  {
   "filename": "anotherfile.mp3"
   "artist": "artist2"
   "title": "title2"
   "genre": "electro"
  }
  </code></pre>
  
  <p>Im trying to figure out how I can sort this array list by the artist element. Currently its just ordering it in the way it is created originally via the filename. Reading on stackoverflow suggests I should be trying to use a custom comparator. Ive been looking at this answer (<a href="https://stackoverflow.com/questions/18575757/how-to-sort-gson-array-based-on-a-key">How to sort GSON Array based on a key?</a>) but its not working for me.</p>
  
  <p>I have a custom class called Tracks.class which has all the getters and setters set up holding title,artist,genre etc</p>
  
  <p>TrackFileManager.class is the serializer and where all the JsonObject.addProperty code is and returns a jsonObject back to MainActivity.</p>
  
  <p>this jsonObject then gets added to an ArrayList called values as below in MainActivity.
  (listOfFiles is another array list that holds all the filenames of my mp3s)</p>
  
  <pre><code>final GsonBuilder gsonBuilder = new GsonBuilder();
  gsonBuilder.registerTypeAdapter(Track.class, new TrackFileManager());
  gsonBuilder.setPrettyPrinting();
  //diasableHtmlEscaping stops &amp; turning into \u0026
  final Gson gson = gsonBuilder.disableHtmlEscaping().create();
  
  values = new ArrayList&lt;String&gt;();
  
  for (String file : listOfFiles){
      t = new Track(path,file);
      values.add(gson.toJson(t));
  }
  Collections.sort(values, new MyJSONComparator());
  </code></pre>
  
  <p>The other code i have is a MyJSONComparator.class which is below</p>
  
  <pre><code>public class MyJSONComparator implements Comparator&lt;String&gt; {
      @Override
      public int compare(String s1, String s2) {
          return s1.compareTo(s2);
      }
  }
  </code></pre>
  
  <p>This produces no errors but it doesnt sort it which I know it wont but its the only way for it not hint errors in Android Studio. If I change the code to <code>implements Comparator&lt;Track&gt;</code> the <code>Collections.sort</code> produces an error. And unless I have comparator I can't access the elements i want to sort on like <code>s1.getArtist</code> etc unless thats not the right way to do it?? I've also tried <code>ArrayList&lt;String&gt;</code> and pretty much all other combinations of types except the one that works.</p>
  
  <p>Im obviously doing something wrong, im pretty sure im almost there.
  Can anyone help or at least point me in the right direction?</p>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>public class MyJSONComparator implements Comparator&lt;Track&gt; {
      @Override
      public int compare(Track s1, Track s2) {
          return s1.artist.compareTo(s2.artist);
      }
  }
  </code></pre>
  
  <p>The above code causes <code>Collections.sort(values,new MyJSONComparator());</code> to error.</p>
  
  <p><code>Sort (List&lt;String&gt;, java.util.Comparator&lt;? super java.lang.string&gt; in Collections cannot be applied to ArrayList&lt;String&gt;, com.example.MyJSONComparator.</code></p>