Sorting List on basis of certain letter I am trying to sort a list on basis of C before .
Is there better way to do this?
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class MainClass {
    public static void main(String args[]) {

    List&lt;String&gt; activeLoadList = new LinkedList&lt;String&gt;();
    List&lt;String&gt; tempList = new LinkedList&lt;String&gt;();
    activeLoadList.add(&quot;REL1110C.PZ&quot;);
    activeLoadList.add(&quot;REL1110Z.PS&quot;);
    activeLoadList.add(&quot;REL1110C.AB&quot;);
    activeLoadList.add(&quot;REL1110Z.CD&quot;);
    activeLoadList.add(&quot;REL1110C.OP&quot;);

    System.out.println(&quot;Queue is ***&quot; + activeLoadList);
    
    Iterator&lt;String&gt; iter = activeLoadList.iterator();
    while (iter.hasNext()) {
        String next = iter.next();
        if (next.contains(&quot;C.&quot;)) {
            iter.remove();
            tempList.add(next);
        }
    }
    
    activeLoadList.addAll(tempList);
    System.out.println(&quot;Queue is ***&quot; + activeLoadList);
  }
}

Output:

Queue is ***[REL1110C.PZ, REL1110Z.PS, REL1110C.AB, REL1110Z.CD, REL1110C.OP]
Queue is ***[REL1110Z.PS, REL1110Z.CD, REL1110C.PZ, REL1110C.AB, REL1110C.OP]
Instead of modifying the structure of the list (adding, removing) you should rather use a Comparator (see the javadoc), which in your case could look like this:

Collections.sort(activeLoadList, new Comparator&lt;String&gt;() {
    @Override
    public int compare(String a, String b) {
        boolean aContainsPattern = a.contains("C.");
        boolean bContainsPattern = b.contains("C.");
        if (aContainsPattern == bContainsPattern){
            return 0;
        } else {
            return aContainsPattern ? 1 : -1;
        }
    }
});


Note though, that you could/should extract your pattern to a constant instead of having it duplicated (like in my example) and furthermore, that you could use a lambda instead of an anonymous inner class if you use Java 8. 

Edit:

In case you use Java 8 (and the ternary operator ?) you could boil that down quite a bit:  

final String p = "C."; // the pattern to search for
Collections.sort(activeLoadList, (a, b) -&gt; (a.contains(p) == b.contains(p) ? 0 : (a.contains(p) ? 1 : -1)));


But for the sake of readability you may rather use the more verbose version. Furthermore Solomonoff's Secret's solution is to prefer in case you won't have any further sorting criteria in the future. If you may have additional ones, though, a Comparator may be the easier way to go.

Edit 2:

As has been pointed out by Solomonoff's Secret this could even be simplified further with Java 8's Comparator.comparing: 

Collections.sort(activeLoadList, Comparator.comparing(text -&gt; text.contains("C.")));


Edit 3:

Thanks to Fabio F. for an even short version using Java 8:

activeLoadList.sort(Comparator.comparing(text -&gt; text.contains("C.")));