title: Sorting of two-dimensional array based on two parameters
body: <p>The following code performs 'hierarchical' sorting of a two-dimensional matrix. Firstly, it sorts elements based on the values of <code>ranks</code>. Secondly, it takes this sorted matrix, searches elements that have the same values of <code>ranks</code>, and sorts them based on <code>dist</code>. In descending order.</p>
  
  <p><strong>Question 1:</strong> Is it possible to achieve the same result in the easier way? I tried to create a <code>Comparator</code>, but it provided incorrect result for this particular case.</p>
  
  <p><strong>Question 2:</strong> How to get indexes of unsorted elements after sorting?</p>
  
  <pre><code>import java.util.ArrayList;
  
  public class Test {
  
      public static void main(String args[]) {
  
          ArrayList&lt;ArrayList&lt;Double&gt;&gt; values = new ArrayList&lt;ArrayList&lt;Double&gt;&gt;();
  
          ArrayList&lt;Double&gt; ranks = new ArrayList&lt;Double&gt;();
          ArrayList&lt;Double&gt; dist = new ArrayList&lt;Double&gt;();
  
          ranks.add(8.0);
          ranks.add(3.0);
          ranks.add(8.0);
          ranks.add(1.0);
  
          dist.add(1.8);
          dist.add(2.8);
          dist.add(1.9);
          dist.add(2.1);
  
          values.add(0,ranks);
          values.add(1,dist);
  
          int len = ranks.size();
  
          ArrayList&lt;ArrayList&lt;Double&gt;&gt; sortedranks = new ArrayList&lt;ArrayList&lt;Double&gt;&gt;();
  
          sortedranks = order(values,0,ranks.size());
  
          boolean swapped = true;
          int j = 0;
          double tmp1, tmp2;
          while (swapped) {
                swapped = false;
                j++;
                for (int i = 0; i &lt; len - j; i++) {  
                    double val1 = sortedranks.get(0).get(i);
                    double val2 = sortedranks.get(0).get(i+1);
                    if (val1==val2) {
                      if (sortedranks.get(1).get(i) &lt; sortedranks.get(1).get(i+1)) {    
                          tmp1 = sortedranks.get(1).get(i);
                          tmp2 = sortedranks.get(1).get(i+1);
                          sortedranks.get(1).remove(i);
                          sortedranks.get(1).remove(i);
                          sortedranks.get(1).add(i,tmp2);
                          sortedranks.get(1).add(i+1,tmp1);
                          swapped = true;
                      }
                    }
                }                
          }
  
          for (int i = 0; i &lt; len; i++) {
              System.out.println("Ranks " + i + " : " + sortedranks.get(0).get(i)
                      + ", Distances : " + sortedranks.get(1).get(i));
          }
  
  
      }
  
      public static ArrayList&lt;ArrayList&lt;Double&gt;&gt; order(ArrayList&lt;ArrayList&lt;Double&gt;&gt; values, int i_start, int i_fin) {
          boolean swapped = true;
          int j = 0;
          int i_rank = 0;
          int i_dist = 1;
          double tmp1_rank, tmp2_rank, tmp1_dist, tmp2_dist;
          while (swapped) {
                swapped = false;
                j++;
                for (int i = i_start; i &lt; i_fin - j; i++) {                                       
                      if (values.get(i_rank).get(i) &lt; values.get(i_rank).get(i+1)) {                          
                            tmp1_rank = values.get(i_rank).get(i);
                            tmp2_rank = values.get(i_rank).get(i+1);
                            tmp1_dist = values.get(i_dist).get(i);
                            tmp2_dist = values.get(i_dist).get(i+1);
                            values.get(i_rank).remove(i);
                            values.get(i_rank).remove(i);
                            values.get(i_dist).remove(i);
                            values.get(i_dist).remove(i);
                            values.get(i_rank).add(i,tmp2_rank);
                            values.get(i_rank).add(i+1,tmp1_rank);
                            values.get(i_dist).add(i,tmp2_dist);
                            values.get(i_dist).add(i+1,tmp1_dist);
                            swapped = true;
                      }
                }                
          }
          return values;
      }
  }
  </code></pre>
  
  <p>The code that uses Comparator (does not work for my case):</p>
  
  <pre><code>public class MyEntry implements Comparable&lt;MyEntry&gt; {
  
      private Double rank;
      private Double dist;
  
      public MyEntry(double rank, double dist) {
  
          this.rank = rank;
          this.dist = dist;
  
      }
  
      public static Comparator&lt;MyEntry&gt; ValueComparator = new Comparator&lt;MyEntry&gt;() {
  
          public int compare(MyEntry value1, MyEntry value2) {
  
              Double rfirst = value1.rank;
              Double rsecond = value2.rank;
  
              Double dfirst = value1.dist;
              Double dsecond = value2.dist;
  
              if (rsecond != rfirst) {
                  return (int) (rsecond - rfirst);
              } 
              else {
                  return (int) (dsecond - dfirst);
              }
  
          }
  
      };
  }
  </code></pre>