Q:

Java: Sorting specific Strings first, then sort by alpha numeric

I try to sort according to:
MyPOJO:
String city;
String tokenA;
String tokenB;

city is a name of a City.
tokenA can only be either "ALARM" or "ALERT"
tokenB can only be either "ALARM" or "ALERT"

MyPOJO with "ALARM" in either tokenA or tokenB or both must come first
MyPOJO with "ALERT" in either tokenA or tokenB or both must come second
last all the cities should come, sorted alphanumeric

If several MyPOJO's has "ALARM" in both tokenA and tokenB, sort according to city within.
If several MyPOJO's has "ALERT" in both tokenA and tokenB, sort according to city within.
A MyPOJO with ALARM in any field or both fields must always come before ALERT.
I have the first tokenA but Im not sure how to best bring in tokenB.
Code I have:
@Override
public int compareTo(Object other) {
    if (other == null) {
        throw new NullPointerException();
    }

    MyPOJO that = (MyPOJO) other;

    int token = tokenA.compareTo(that.tokenA);
    if (token != 0) {
        return token;
    }

    return city.compareTo(that.city);
}

A:

Here is the method you want.
In the compareTo method do this:
if tokenA == ALARM and other.tokenA != ALARM return -1
if tokenA != ALARM and other.tokenA == ALARM return 1
if tokenB == ALARM and other.tokenB != ALARM return -1
if tokenB != ALARM and other.tokenB == ALARM return 1
if tokenB == ALARM and other.tokenB == ALARM
  return city.compareTo(other.city)
if tokenA == ALERT and other.tokenA != ALERT return -1
if tokenA != ALERT and other.tokenA == ALERT return 1
if tokenB == ALERT and other.tokenB != ALERT return -1
if tokenB != ALERT and other.tokenB == ALERT return 1
if tokenB == ALERT and other.tokenB == ALERT 
  return city.compareTo(other.city)
return city.compareTo(other.city)

