Sort a list with another list

Is it possible to use the Java Collections sorting method with a comparator, since it sorts one list and also sorts another list by the index of the original list so that the lists remain paired? Thank.

+5
source share
2 answers

You cannot do this with a comparator. The solution to your problem is to create a third list containing pairs of matching items from the specified lists. Then sort and copy back to the source lists.

public class Pair<X,Y> {
  public final X x;
  public final Y y;

  public Pair(X x, Y y) {
    this.x = x; this.y = y;
  }
}

public static<X,Y> void sortTwoLists(List<X> xs, List<Y> ys, final Comparator<X> c) {
 if (xs.size() != ys.size()) 
   throw new RuntimeException("size mismatch");

 List<Pair<X,Y>> temp = new ArrayList<Pair<X,Y>>();

 for (int i = 0; i < xs.size(); ++i) 
   temp.add(new Pair<X,Y>(xs.get(i), ys.get(i)));

 Collections.sort(temp, new Comparator<Pair<X,Y>>() {
  @Override
  public int compare(Pair<X, Y> a, Pair<X, Y> b) {
    return c.compare(a.x, b.x);
  }
 });

 for(int i = 0; i < xs.size(); ++i) {
   xs.set(i, temp.get(i).x);
   ys.set(i, temp.get(i).y);
 }
}
+4
source

, , sort, . .

, , . " " - "" . , .

+4

All Articles