fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone{
  9.  
  10. public static <T extends Comparable<T>> void concurrentSort( final List<T> key, List<?>... lists){
  11. // Do validation
  12. if(key == null || lists == null)
  13. throw new NullPointerException("key cannot be null.");
  14.  
  15. for(List<?> list : lists)
  16. if(list.size() != key.size())
  17. throw new IllegalArgumentException("all lists must be the same size");
  18.  
  19. // Lists are size 0 or 1, nothing to sort
  20. if(key.size() < 2)
  21. return;
  22.  
  23. // Create a List of indices
  24. List<Integer> indices = new ArrayList<Integer>();
  25. for(int i = 0; i < key.size(); i++)
  26. indices.add(i);
  27.  
  28. // Sort the indices list based on the key
  29. Collections.sort(indices, new Comparator<Integer>(){
  30. @Override public int compare(Integer i, Integer j) {
  31. return key.get(i).compareTo(key.get(j));
  32. }
  33. });
  34.  
  35. Map<Integer, Integer> swapMap = new HashMap<Integer, Integer>(indices.size());
  36.  
  37. // create a mapping that allows sorting of the List by N swaps.
  38. for(int i = 0; i < indices.size(); i++){
  39. int k = indices.get(i);
  40. while(swapMap.containsKey(k))
  41. k = swapMap.get(k);
  42.  
  43. swapMap.put(i, k);
  44. }
  45.  
  46. // for each list, swap elements to sort according to key list
  47. for(Map.Entry<Integer, Integer> e : swapMap.entrySet())
  48. for(List<?> list : lists)
  49. Collections.swap(list, e.getKey(), e.getValue());
  50. }
  51.  
  52. ///////////////////////////////////////////////
  53. // Output
  54. ///////////////////////////////////////////////
  55.  
  56. public static void main (String[] args) throws java.lang.Exception{
  57. List<String> list1 = Arrays.asList("MURDER!","It's", "Hello","Yes-Man", "ON");
  58. List<String> list2 = Arrays.asList("demrru", "ist", "ehllo", "aemnsy", "no");
  59. List<Integer> list3 = Arrays.asList(2, 4, 3, 1, 5);
  60. List<Double> list4 = Arrays.asList(0.2, 0.4, 0.3, 0.1, 0.5);
  61.  
  62. System.out.println("======= Before Sort ========");
  63. System.out.println(list1);
  64. System.out.println(list2);
  65. System.out.println(list3);
  66. System.out.println(list4);
  67. System.out.println();
  68. System.out.println();
  69.  
  70. System.out.println("======= Sort By List2 =========");
  71. concurrentSort(list2, list1, list2, list3, list4);
  72. System.out.println(list1);
  73. System.out.println(list2);
  74. System.out.println(list3);
  75. System.out.println(list4);
  76. }
  77. }
Success #stdin #stdout 0.1s 320320KB
stdin
Standard input is empty
stdout
======= Before Sort ========
[MURDER!, It's, Hello, Yes-Man, ON]
[demrru, ist, ehllo, aemnsy, no]
[2, 4, 3, 1, 5]
[0.2, 0.4, 0.3, 0.1, 0.5]


======= Sort By List2 =========
[Yes-Man, MURDER!, Hello, It's, ON]
[aemnsy, demrru, ehllo, ist, no]
[1, 2, 3, 4, 5]
[0.1, 0.2, 0.3, 0.4, 0.5]