title: Why does collections.sort throw unsupported operation exception while sorting by comparator in Java?
body: <p>Following is my code used to sort a list with predefined order. Defined order is mentioned in itemsSorted list.</p>
  
  <pre><code>final List&lt;String&gt; itemsSorted = myMethod.getSortedItems();
  
  List&lt;String&gt; plainItemList = myMethod2.getAllItems();
  
  final Comparator&lt;String&gt; comparator = new Comparator&lt;String&gt;() {        
  
      public int compare(String str1, String str2) {
          return orderOf(str1) - orderOf(str2);
      }
  
      private int orderOf(String name) {          
          return ((itemsSorted)).indexOf(name);
      }
   };
   Collections.sort(plainItemList, comparator);
   return plainItemList;
  </code></pre>
  
  <p>The above code throws </p>
  
  <pre><code>Caused by: java.lang.UnsupportedOperationException
      at java.util.Collections$UnmodifiableList$1.set(Collections.java:1244)
      at java.util.Collections.sort(Collections.java:221)
  </code></pre>
  
  <p>I'm not sure why the list is unmodifiable. Please help me on this.</p>