/* 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());

        // create a mapping that allows sorting of the List by N swaps.
		for(int i = 0; i < indices.size(); i++){
			int k = indices.get(i);
			while(swapMap.containsKey(k))
				k = swapMap.get(k);
			
			swapMap.put(i, k);
		}
		
        // for each list, swap elements to sort according to key list
        for(Map.Entry<Integer, Integer> e : swapMap.entrySet())
			for(List<?> list : lists)
				Collections.swap(list, e.getKey(), e.getValue());
	}
	
	///////////////////////////////////////////////
	// Output
	///////////////////////////////////////////////
	
	public static void main (String[] args) throws java.lang.Exception{
		List<String>  list1 = Arrays.asList("MURDER!","It's", "Hello","Yes-Man", "ON");
    	List<String>  list2 = Arrays.asList("demrru", "ist", "ehllo", "aemnsy", "no");
    	List<Integer> list3 = Arrays.asList(2, 4, 3, 1, 5);
    	List<Double>  list4 = Arrays.asList(0.2, 0.4, 0.3, 0.1, 0.5);
		
		System.out.println("======= Before Sort ========");
		System.out.println(list1);
		System.out.println(list2);
		System.out.println(list3);
		System.out.println(list4);
		System.out.println();
		System.out.println();
		
		System.out.println("======= Sort By List2 =========");
    	concurrentSort(list2, list1, list2, list3, list4);
		System.out.println(list1);
		System.out.println(list2);
		System.out.println(list3);
		System.out.println(list4);
	}
}