Sorting a list but the sort order isn't known until runtime I need to sort a list of strings that I'm reading in but the sorted order of that list is not predetermined (I can't hard code it); I read in the sort order from a source and build a comparator by assigning a numerical value to each entry in a map and then use that to determine the order.
My implementation is pretty simple and straightforward. I don't know if there is any room to optimize this but given that sorting is generally a slow process in itself, I wanted to submit this for code review in case there are in fact any improvements to be made.
The custom comparator will be cached after creation so I won't be building a new one unless required to do so.
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class RunTest {
   // Runnable test
   public static void main(String[] args) {
      CustomComparator comparator = CustomComparator.fromList(getSortOrder());
      
      List&lt;String&gt; list1 = getTestList();
      
      System.out.println(&quot;Test list: \n  &quot; + list1);
      
      Collections.sort(list1, comparator);
      
      System.out.println(&quot;Test list (sorted): \n  &quot; + list1);
   }
   
   // Comparator
   public static class CustomComparator implements Comparator&lt;String&gt; {
      private Map&lt;String, Integer&gt; _sortOrder;
      
      public CustomComparator(Map&lt;String, Integer&gt; sortOrder) {
         _sortOrder = sortOrder;
      }
      
      @Override
      public int compare(String l, String r) {
         Integer lvalue = _sortOrder.get(l);
         Integer rvalue = _sortOrder.get(r);
         
         if (lvalue == null || rvalue == null)
            return 0;
         
         if (lvalue &lt; rvalue)
            return -1;
         
         return 1;
      }
      
      public static CustomComparator fromList(List&lt;String&gt; sortOrder) {
         // Map each list item to a comparable integer
         Map&lt;String, Integer&gt; customSortOrder = new HashMap&lt;&gt;();
         
         int i = 0;
         
         for (String s : sortOrder)
            customSortOrder.put(s, i++);
         
         return new CustomComparator(customSortOrder);
      }
   }
   
   // Custom sort order
   public static List&lt;String&gt; getSortOrder() {
      List&lt;String&gt; customSortOrder = new LinkedList&lt;&gt;();
      
      customSortOrder.add(&quot;c&quot;);
      customSortOrder.add(&quot;b&quot;);
      customSortOrder.add(&quot;a&quot;);
      
      return customSortOrder;
   }
   
   // Test list
   public static List&lt;String&gt; getTestList() {
      List&lt;String&gt; randomList = new LinkedList&lt;&gt;();
      
      randomList.add(&quot;b&quot;);
      randomList.add(&quot;a&quot;);
      randomList.add(&quot;c&quot;);
      
      return randomList;
   }
}

Output:

Test list:
  [b, a, c]
Test list (sorted):
  [c, b, a]
This:
     if (lvalue == null || rvalue == null)
        return 0;

doesn't seem very safe. It says that if either of the values does not have a defined order, then don't care about the order, consider them equivalent. This will have the effect of interleaving such items throughout your sorted list.
Technically this fails the requirement of Comparator that

The implementor must also ensure that the relation is transitive

In other words, given some strings a, b and c with ordering indices 1, null and 2, your existing implementation will say that a == b, b == c but a != c - this is non-transitive.
Instead: either throw an exception if there is a value with an undefined order; or choose to default undefined-order values at the beginning or end of the list.