0

There are 5 entries stored in ArrayList<ArrayList<Double>> selected. Each of these entries is specified by two parameters - rank and cd:

rank = [1.0, 2.0, 3.1, 1.2, 2.1]
cd = [6.2, 5.2, 7.1, 8.0, 1.1]

I need to order these entries, firstly, by rank and, secondly, by cd in descending order (i.e. 3.1, 2.1, 2.0, 1.2, 1.1). The second ordering (by cd) must be applied to entries that have been already ordered by rank.

ArrayList<Double> rank = new ArrayList<Double>(); 
ArrayList<Double> cd = new ArrayList<Double>();

ArrayList<ArrayList<Double>> selected = new ArrayList<ArrayList<Double>>();

for (int i=0; i<len; i++) {
    rank.add(getRank(i));
    cd.add(getCub_len(i));
}
selected.add(0,rank);
selected.add(1,cd);

Comparator<ArrayList<Double>> comparatorRank = new Comparator<ArrayList<Double>>() 
{
    public int compare(ArrayList<Double> a, ArrayList<Double> b) 
    {
        return (int) (a.get(0) - b.get(0));
    }
};

Comparator<ArrayList<Double>> comparatorCD = new Comparator<ArrayList<Double>>() 
{
    public int compare(ArrayList<Double> a, ArrayList<Double> b) 
    {
        return (int) (a.get(1) - b.get(1));
    }
};

Collections.sort(selected, comparatorRank);
Collections.sort(selected, comparatorCD);

The problem is that I don't know how to get IDs that have been assigned to entries before ordering. For instance, this is unordered sequence of IDs: 1, 2, 3, 4, 5, and this is the sequence of IDs after ordering: 5, 3, 4, 1, 2. How to get these IDs?

Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217
  • 2
    Seems like you need a class that stores the rank and code of a single object so that you can have a list of that object. Then you just define your compareTo of that class to use the rank as the field to sort on. – Kevin Crowell Mar 27 '13 at 19:22
  • @Kevin Crowell: Could you provide some example please? Thanks. – Klausos Klausos Mar 27 '13 at 19:25
  • Check the accepted answer to this question: http://stackoverflow.com/questions/3718383/java-class-implements-comparable – Kevin Crowell Mar 27 '13 at 22:06

1 Answers1

0

This is how I would approach it...

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SortPairs {

    public static void main(String[] args) {
        List<Double> rank = new ArrayList<Double>(Arrays.asList(1.0, 2.0, 3.1, 1.2, 2.1)); 
        List<Double> cd = new ArrayList<Double>(Arrays.asList(6.2, 5.2, 7.1, 8.0, 1.1));

        List<Pair<Double, Double>> pairs = new ArrayList<Pair<Double, Double>>(rank.size());
        for (int i = 0; i < rank.size(); ++i)
            pairs.add(new Pair<Double, Double>(rank.get(i), cd.get(i)));

        Collections.sort(pairs);

        ListIterator<Double> rankIter = rank.listIterator();
        ListIterator<Double> cdIter = cd.listIterator();
        for (Pair<Double, Double> pair : pairs) {
            System.out.println(String.format("[rank = %.1f, cd = %.1f", pair.getT1(), pair.getT2()));
            rankIter.next();
            cdIter.next();
            rankIter.set(pair.getT1());
            cdIter.set(pair.getT2());
        }
    }

   public static class Pair <T1 extends Comparable<? super T1>, T2 extends Comparable<? super T2>> implements Comparable<Pair<T1, T2>> {
      private final T1 t1;
      private final T2 t2;

      public Pair(T1 t1, T2 t2) {
         this.t1 = t1;
         this.t2 = t2;
      }

      public T1 getT1() {
          return t1;
      }

      public T2 getT2() {
          return t2;
      }

      @Override
      public int compareTo(Pair<T1, T2> other) {
          return t1.equals(other.t1) ?
                 other.t2.compareTo(t2) : 
                 other.t1.compareTo(t1);
      }
   }
}
pscuderi
  • 1,554
  • 12
  • 14