title: Sorting list based on another list's order
body: <p>I need to sort a list of <code>Person</code> objects(<code>List&lt;Person&gt;</code>, where each <code>Person</code> object has few attributes like <code>id</code>(unique), <code>name</code>, <code>age</code>… etc).</p>
  <p>The sorting order is based on another list. That list contains a set of <code>Person</code> <code>id</code>'s (A <code>List&lt;String&gt;</code> which is already sorted).</p>
  <p>What is the best way to order the <code>List&lt;Person&gt;</code> in the same order as the list of <code>id</code>'s using Kotlin or Java.</p>
  <p>Example:</p>
  <pre><code>List Person {
  (“ID1”,”PERSON1”,22,..), (“ID-2”,”PERSON2”,20,..) ), (“ID-3”,”PERSON3”,19,..),…..
  }
  </code></pre>
  <p>Ordered Id List :</p>
  <pre><code>List of ID {(“ID2”), (“ID1”),(”ID3”)….}
  </code></pre>
  <p>Sorted <code>Person</code> list should be:</p>
  <pre><code>List PERSON {
   (“ID-2”,”PERSON 2”,20,..) ), (“ID1”,”PERSON 2”,22,..),  (“ID-3”,”PERSON 2”,19,..),…..
  }
  </code></pre>
  <p>If the <code>Person</code> list contains any <code>id</code>'s which are not mentioned in the <code>id</code> list then those values should be at the end of the sorted list.</p>
  <hr />
  <p>Edited: This is my current way in Java. I am hoping for a better way than this:</p>
  <pre><code>public static List&lt;Person&gt; getSortList(List &lt;Person&gt; unsortedList, List&lt;String&gt; orderList){
  
      if(unsortedList!=null &amp;&amp; !unsortedList.isEmpty() &amp;&amp; orderList!=null &amp;&amp; !orderList.isEmpty()){
          List sortedList = new ArrayList&lt;OpenHABWidget&gt;();
          for(String id : orderList){
              Person found= getPersonIfFound(unsortedList, id); // search for the item on the list by ID
              if(found!=null)sortedList.add(found);       // if found add to sorted list
              unsortedList.remove(found);        // remove added item
          }
          sortedList.addAll(unsortedList);        // append the reaming items on the unsorted list to new sorted list
          return sortedList;
      }
      else{
          return unsortedList;
      }
  
  }
  
  public static Person getPersonIfFound(List &lt;Person&gt; list, String key){
      for(Person person : list){
          if(person.getId().equals(key)){
              return person;
          }
      }
      return null;
  }
  </code></pre>