I am sorting my custom objects. Custom object contain state and timestamp. First I have to sort against state then timestamp. State having below values
Running, Waiting, Registering, Completed and Aborted
So state having value Running should come on top then Waiting and so on.
If I have to sort alphabetically then I can do easily via
state1.compareTo(state2)
But can I sort with this criteria. Please help me to write this logic.
EDIT
As you people suggested I took Enum
private enum TournamentState {
Running,
Waiting,
Registering,
Completed,
Aborted
}
And compare like below
int sort = EnumState.valueOf(status1).compareTo(EnumState.valueOf(status2));
if(sort != 0){
return sort;
}else{
return (int) (time1 - time2);
}
Thank you very much for your support.
state1.compareTo(state2)returns 0, meaning both states are equal, and then either compare the timestamp for equal states or return the result of the state comparision for unequal states.Comparator, what does yours look like right now?