title: Android: Sorting integer values in ListView
body: <p>I am making a HighScore Listview. The ListView has 2 elements, the Name of the player and the Score. The score is an integer value. I am using Collections.sort, however, upon starting the activity, the list isn't sorted. I have already loaded dummy values for the list. These are declared in strings.xml as string arrays. Here is the snippet for my onCreate:</p>
  
  <pre><code>   @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      backgroundMusic = MediaPlayer.create(HighScores.this, R.raw.highscore);
      backgroundMusic.setLooping(true);
      backgroundMusic.start();
  
      String databasePath = getAbsolutePath("highscore.dbs");
  
      // open the database
      try {
          db = StorageFactory.getInstance().createStorage();
          db.open(databasePath, 40 * 1024);
          Toast.makeText(this, "HERE", Toast.LENGTH_SHORT).show();
      } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
      // check if a root object is present in this file
      Index&lt;FetchDetails&gt; root = (Index&lt;FetchDetails&gt;) db.getRoot();
      if (root == null) {
          // Root is not yet defined: storage is not initialized
          root = (Index) db.createIndex(String.class, false);
          String[] nameList = getResources().getStringArray(
                  R.array.name_array);
          String[] scoreList = getResources().getStringArray(
                  R.array.score_array);
          for (int i = 0; i &lt; scoreList.length; i++) {
              FetchDetails populate = new FetchDetails();
              populate.setName(nameList[i]);
              populate.setScore(scoreList[i]);
              root.put(populate.getScore(), populate);
              db.setRoot(root);
          }
  
      }
  
      String filter = "";
      ArrayList&lt;FetchDetails&gt; items = root.getPrefixList(filter);
      results = new ArrayList&lt;FetchDetails&gt;();
      ScoreComparator compare = new ScoreComparator();
      for (int i = 0; i &lt; items.size(); i++) {
          FetchDetails arraylist = new FetchDetails();
          arraylist.setName(items.get(i).getName());
          arraylist.setScore(items.get(i).getScore());
          results.add(arraylist);
          Collections.sort(results, compare);
      }
  
      adapter = new CustomAdapter(results);
      setListAdapter(adapter);
      updateList();
  
  }
  </code></pre>
  
  <p>My updateList:</p>
  
  <pre><code>   private void updateList() {
      // TODO Auto-generated method stub
      Intent updateIntent = getIntent();
      if ((updateIntent.getStringExtra(HighScores.NAME) != null)
              &amp;&amp;   (updateIntent.getStringExtra(MegamanStrikes.PLAYER_SCORE) != null)) {
          FetchDetails updateList = new FetchDetails();
          ScoreComparator compare = new ScoreComparator();
          updateList.setName(updateIntent.getStringExtra(HighScores.NAME));
          updateList.setScore(updateIntent
                  .getStringExtra(MegamanStrikes.PLAYER_SCORE));
  
          Toast.makeText(this, updateIntent.getStringExtra(MegamanStrikes.PLAYER_SCORE), Toast.LENGTH_SHORT).show();
          Toast.makeText(this, updateIntent.getStringExtra(HighScores.NAME), Toast.LENGTH_SHORT).show();
  
          results.add(updateList);
          adapter.notifyDataSetChanged();
          Collections.sort(results, compare);
  
          Index&lt;FetchDetails&gt; rootEdit = (Index&lt;FetchDetails&gt;) db.getRoot();
          rootEdit.put(updateList.getScore(), updateList);
          db.setRoot(rootEdit);
      }
  
  }
  </code></pre>
  
  <p>My Fetch Details class</p>
  
  <pre><code>package com.cs119.megamanstrikes;
  
  import org.garret.perst.Persistent;
  
  public class FetchDetails extends Persistent implements Comparable&lt;FetchDetails&gt; {
  
      private String name;
      private String score;
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  public String getScore() {
      return score;
  }
  
  public void setScore(String score) {
      this.score = score;
  }
  
  @Override
  public int compareTo(FetchDetails another) {
      // TODO Auto-generated method stub
      return score.compareTo(another.score);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>My Custom Adapter</p>
  
  <pre><code>  private class CustomAdapter extends BaseAdapter {
  
      Index&lt;FetchDetails&gt; root = (Index&lt;FetchDetails&gt;) db.getRoot();
      String filter = "";
      ArrayList&lt;FetchDetails&gt; items = root.getPrefixList(filter);
  
      public CustomAdapter(ArrayList&lt;FetchDetails&gt; highscore) {
          items = highscore;
      }
  
      @Override
      public int getCount() {
          // TODO Auto-generated method stub
          return items.size();
      }
  
      @Override
      public Object getItem(int index) {
          // TODO Auto-generated method stub
          return items.get(index);
      }
  
      @Override
      public long getItemId(int position) {
          // TODO Auto-generated method stub
          return position;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
  
          LayoutInflater inflater = getLayoutInflater();
          View view;
  
          if (convertView == null) {
              view = inflater.inflate(R.layout.row, null);
          } else {
              view = convertView;
          }
  
          // extract the views to be populated
          TextView Name = (TextView) view.findViewById(R.id.name);
          TextView Score = (TextView) view.findViewById(R.id.score);
  
          FetchDetails score = items.get(position);
  
          Name.setText(score.getName());
          Score.setText(score.getScore());
  
          // return the view
          return view;
      }
  }
  </code></pre>
  
  <p>My Comparator class used as second parameter for sort (Im not even sure if this is needed!)</p>
  
  <pre><code>            package com.cs119.megamanstrikes;
  
  import java.util.Comparator;
  
  
  class ScoreComparator implements Comparator&lt;FetchDetails&gt; {
  
  @Override
  public int compare(FetchDetails objA, FetchDetails objB) {
      // TODO Auto-generated method stub
      return objA.getScore().compareTo(objB.getScore());
  }
  }
  </code></pre>
  
  <p>And yet the output is still like this:</p>
  
  <p><img src="https://i.stack.imgur.com/va6ss.png" alt="enter image description here"></p>
  
  <p>Is there a way to remedy this?</p>