/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone{
	
	public static <T extends Comparable<T>> void concurrentSort( final List<T> key, List<?>... lists){
        // Do validation
        if(key == null || lists == null)
        	throw new NullPointerException("key cannot be null.");
        
        for(List<?> list : lists)
        	if(list.size() != key.size())
        		throw new IllegalArgumentException("all lists must be the same size");
        
        // Lists are size 0 or 1, nothing to sort
        if(key.size() < 2)
        	return;
        
        // Create a List of indices
		List<Integer> indices = new ArrayList<Integer>();
		for(int i = 0; i < key.size(); i++)
			indices.add(i);

        // Sort the indices list based on the key
		Collections.sort(indices, new Comparator<Integer>(){
			@Override public int compare(Integer i, Integer j) {
				return key.get(i).compareTo(key.get(j));
			}
		});
		
		Map<Integer, Integer> swapMap = new HashMap<Integer, Integer>(indices.size());
		List<Integer> swapFrom = new ArrayList<Integer>(indices.size()),
					  swapTo   = new ArrayList<Integer>(indices.size());

        // create a mapping that allows sorting of the List by N swaps.
		for(int i = 0; i < key.size(); i++){
			int k = indices.get(i);
			while(i != k && swapMap.containsKey(k))
				k = swapMap.get(k);
			
			swapFrom.add(i);
			swapTo.add(k);
			swapMap.put(i, k);
		}
		
        // use the swap order to sort each list by swapping elements
		for(List<?> list : lists)
			for(int i = 0; i < list.size(); i++)
				Collections.swap(list, swapFrom.get(i), swapTo.get(i));
	}
	
	public static void main (String[] args) throws java.lang.Exception{
		List<Integer> key = Arrays.asList(4, 3, 1, 2, 1);
		
		// Dupes are allowed
		List<String> list1 = Arrays.asList("Four", "Three", "One", "Two", "One*");
		
		// List Types do not need to be the same
		List<Character> list2 = Arrays.asList('d', 'c', 'a', 'b', 'a');
		
		System.out.println("======= Before Sort ========");
		System.out.println(key);
		System.out.println(list1);
		System.out.println(list2);
		System.out.println();
		System.out.println();
		
		// Include key in the values parameter if you want it to be sorted too.
		concurrentSort(key, key, list1, list2);
		
		System.out.println("======= After Sort =========");
		System.out.println(key);
		System.out.println(list1);
		System.out.println(list2);
	}
}