title: Sort an Array based on Another Array with different values
body: <p>I want to sort an array based on another array with different values.
  Consider the following:</p>
  
  <p>Reference Array: <code>{"A","B","C"}</code></p>
  
  <p>Obtained Array1: <code>{"C","A","B"}</code><br>
  Obtained Array2: <code>{"cc","aa","bb"}</code><br>
  Obtained Array3: <code>{"123","asrd","sdhg"}</code></p>
  
  <p>values corresponding to</p>
  
  <pre><code>A -&gt; aa and asrd
  B -&gt; bb and sdhg
  C -&gt; cc and 123
  </code></pre>
  
  <p>I want to sort my obtained array 1, 2 and 3 in the order specified by my reference array.</p>
  
  <p>Expected Result:<br>
  Obtained Array after sorting: <code>{"A","B","C"}</code><br>
  Obtained Array2 after sorting: <code>{"aa","bb","cc"}</code><br>
  Obtained Array3 after sorting: <code>{"asrd","sdhg","123"}</code></p>
  
  <p>PS: The reference array elements order can be anything (<code>[A,B,C]</code> or <code>[C,B,A</code>] etc). Obtained arrays 2 and 3 should be sorted accordingly.</p>
  
  <p>I know how to sort Obtained Array 1 in the order of my reference array. I tried a logic to sort obtained arrays 2 and 3 but the result i get is wrong</p>
  
  <pre><code>String[] referenceArray = new String[] { "A", "B", "C" };
  String[] obtainedArray1 = new String[] { "C", "A", "B" };
  String[] obtainedArray2 = new String[] { "cc", "aa", "bb" };
  String[] obtainedArray3 = new String[] { "123", "asrd", "sdhg" };
  
  final List&lt;String&gt; referenceArrayList= Arrays.asList(referenceArray);
  ArrayList&lt;String&gt; obtainedArray1_List= new ArrayList&lt;String&gt;(Arrays.asList(obtainedArray1));
  ArrayList&lt;String&gt; obtainedArray2_List= new ArrayList&lt;String&gt;(Arrays.asList(obtainedArray2));
  ArrayList&lt;String&gt; obtainedArray3_List= new ArrayList&lt;String&gt;(Arrays.asList(obtainedArray3));
  
  
  // Sorting ObtainedArray1_List - This works Fine
  Collections.sort(obtainedArray1_List, Comparator.comparing(s -&gt; referenceArrayList.indexOf(s)));
  
  //Sorting obtainedArray2_List - Not Working
  Collections.sort(obtainedArray2_List, Comparator.comparing(s -&gt; referenceArrayList.indexOf(obtainedArray1[obtainedArray2_List.indexOf(s)])));
  </code></pre>
  
  <p>The result of obtainedArray2_List  after sorting: <code>["aa","cc","bb"]</code><br></p>
  
  <blockquote>
    <p>Expected Result is <code>["aa","bb","cc"]</code></p>
  </blockquote>