Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 0.71 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Sorting a list with another list
  2. public class Pair<X,Y> {
  3.   public final X x;
  4.   public final Y y;
  5.  
  6.   public Pair(X x, Y y) {
  7.     this.x = x; this.y = y;
  8.   }
  9. }
  10.  
  11. public static<X,Y> void sortTwoLists(List<X> xs, List<Y> ys, final Comparator<X> c) {
  12.  if (xs.size() != ys.size())
  13.    throw new RuntimeException("size mismatch");
  14.  
  15.  List<Pair<X,Y>> temp = new ArrayList<Pair<X,Y>>();
  16.  
  17.  for (int i = 0; i < xs.size(); ++i)
  18.    temp.add(new Pair<X,Y>(xs.get(i), ys.get(i)));
  19.  
  20.  Collections.sort(temp, new Comparator<Pair<X,Y>>() {
  21.   @Override
  22.   public int compare(Pair<X, Y> a, Pair<X, Y> b) {
  23.     return c.compare(a.x, b.x);
  24.   }
  25.  });
  26.  
  27.  for(int i = 0; i < xs.size(); ++i) {
  28.    xs.set(i, temp.get(i).x);
  29.    ys.set(i, temp.get(i).y);
  30.  }
  31. }