Sorting String differently on some conditional
<p>I wondered how I could change ordering-direction only on some contitional. In my case Strings starting with 'BB' should be ordered in the other direction, everything else should be ordered as usual. </p>

<p>My Test-Class:</p>

<pre><code>public class StringTest {


    public static void main(String[] args) {
        SomeClass someClass1= new SomeClass("AA");
        SomeClass someClass2= new SomeClass("AB");
        SomeClass someClass3= new SomeClass("CB4");
        SomeClass someClass4= new SomeClass("BB7");
        SomeClass someClass5= new SomeClass("BB9");
        SomeClass someClass6= new SomeClass("BB3");
        SomeClass someClass7= new SomeClass("CB3");

        List&lt;SomeClass&gt; list = new ArrayList&lt;SomeClass&gt;();
        list.add(someClass1);
        list.add(someClass2);
        list.add(someClass3);
        list.add(someClass4);
        list.add(someClass5);
        list.add(someClass6);
        list.add(someClass7);

        Collections.sort(list);

        for (SomeClass someClass : list) {
            System.out.println(someClass.getSomeField());
        }
    }

}
</code></pre>

<p>My Comparator:</p>

<pre><code>public class SomeClass implements Comparable&lt;SomeClass&gt;
{
  private String someField;

  public int compareTo(final SomeClass o)
  {
    int res = 0;

    if (someField.startsWith("BB"))
    {
      res = o.someField.compareTo(someField);
    }
    else
    {
      res = someField.compareTo(o.someField);
    }
    return res;
  }

}
</code></pre>

<p>My desired output:<br>
AA<br>
AB<br>
BB9<br>
BB7<br>
BB3<br>
CB3<br>
CB4</p>

<p>The actual result so far:<br>
AA
AB<br>
CB3<br>
BB9<br>
BB7<br>
BB3<br>
CB4</p>

<p>Jonny</p>
