title: Sorting a list based on another list's values - Java
body: <p>One list with names:(unsorted) e.g <strong>[paul, foul, mark]</strong></p>
  
  <p>Another list with integers:    e.g  <strong>[5, 2, 6]</strong></p>
  
  <p>The values on the second list are the numbers "selected" by each person(name), so
  paul has number 5, foul's number is 2, mark's number is 6. </p>
  
  <p>I'm trying to sort the names' list based on the values of the second list on a descending order. I cant use a map as i need both lists on other occasions on my program. </p>
  
  <p>With the sorting method i did <strong>i get</strong> a list like that:  <strong>[paul, mark, foul]</strong></p>
  
  <p>As you can see, its not sorted as i would want.</p>
  
  <p>The <strong>correct</strong> one would be:  <strong>[mark,paul,foul]</strong></p>
  
  <p>But i cant find the fault on the code.</p>
  
  <pre><code>public ArrayList&lt;String&gt; sortNames(ArrayList&lt;Integer&gt; results){
      String tmp;
      for (int k=0; k&lt;Names.size()-1; k++) {
  
          boolean isSorted=true;
          for (int i=1; i&lt;Names.size()-k; i++) {
  
               if (results.get(i)&gt;results.get(i-1)  ) {
  
                  tmp=Names.get(i);
                  Names.set(i,Names.get(i-1));
                  Names.set(i-1,tmp);
  
                  isSorted=false;
              }
          }
          if (isSorted) break;
      }
      return Names;
  
  }
  </code></pre>
  
  <p><strong>EDIT!!! with the help of the answers below,the code is:</strong></p>
  
  <pre><code>    public ArrayList&lt;String&gt; sortNames(ArrayList&lt;Integer&gt; results){
          String tmp2;
          int tmp;
          for (int k=0; k&lt;Names.size()-1; k++) {
  
              boolean isSorted=true;
              for (int i=1; i&lt;Names.size()-k; i++) {
  
                   if (results.get(i)&gt;results.get(i-1)  ) {
                       tmp=results.get(i);
                       results.set(i,results.get(i-1));
                       results.set(i-1,tmp);
  
  
                      tmp2=Names.get(i);
                      Names.set(i,Names.get(i-1));
                      Names.set(i-1,tmp2);
  
                      isSorted=false;
                  }
              }
              if (isSorted) break;
          }
      return Names;
  
  }
  </code></pre>
  
  <p>This code works properly(for small lists)
  I've just the query why it doesnt work for objects like ImageIcon. Any ideas?</p>