I know this is a much longer solution but counterbalancing it somewhat flexible then just this:
public class Person
{
int compareTo( Person p )
{
int retValue = first_name.compareTo( p.first_name );
if( retValue == 0 )
{
int retValue = last_name.compareTo( p.larst_name );
if ( retValue == 0 )
retValue = Integer.compare( age, p.age );
return retValue;
}
}
}
I created a Person specific comparator class with a mode field (which field of the Person should be compared). If its result is zero it calls its subComparator (if there is one). In this way the caller (main function) decides the ordering mode (how the comparators nest together) and the orderind direction:
The Person class:
public class Person
{
public String first_name;
public String last_name;
public int age;
public Person( String first_name_, String last_name_, int age_ )
{
super();
first_name = first_name_;
last_name = last_name_;
age = age_;
}
}
The PersonComparator class:
public class PersonComparator implements Comparator<Person>
{
public static final int COMPARE_BY_FIRSTNAME = 0;
public static final int COMPARE_BY_LASTNAME = 1;
public static final int COMPARE_BY_AGE = 2;
int mode;
boolean desc;
private final Comparator<Person> subComparator;
public PersonComparator( int mode_, Comparator<Person> subComparator_ )
{
super();
mode = mode_;
subComparator = subComparator_;
}
public PersonComparator( int mode_, Comparator<Person> subComparator_, boolean desc_ )
{
super();
mode = mode_;
desc = desc_;
subComparator = subComparator_;
}
@Override
public int compare( Person p1, Person p2 )
{
int retValue = 0;
switch ( mode )
{
case PersonComparator.COMPARE_BY_FIRSTNAME:
{
retValue = compareByFirstName( p1, p2 );
break;
}
case PersonComparator.COMPARE_BY_LASTNAME:
{
retValue = compareByLastName( p1, p2 );
break;
}
case PersonComparator.COMPARE_BY_AGE:
{
retValue = compareByAge( p1, p2 );
break;
}
}
if ( desc )
return -retValue;
else
return retValue;
}
protected int compareByFirstName( Person p1, Person p2 )
{
int retValue = p1.first_name.compareTo( p2.first_name );
if ( retValue == 0 )
if ( subComparator != null )
retValue = subComparator.compare( p1, p2 );
else
retValue = 0;
return retValue;
}
protected int compareByLastName( Person p1, Person p2 )
{
int retValue = p1.last_name.compareTo( p2.last_name );
if ( retValue == 0 )
if ( subComparator != null )
retValue = subComparator.compare( p1, p2 );
else
retValue = 0;
return retValue;
}
protected int compareByAge( Person p1, Person p2 )
{
int retValue = p1.age - p2.age;
if ( retValue == 0 )
if ( subComparator != null )
retValue = subComparator.compare( p1, p2 );
return retValue;
}
}
The caller:
public class PersonComparatorApp
{
public static void main( String[] args )
{
List<Person> l = new ArrayList<>();
l.add( new Person( "b", "c", 1 ) );
l.add( new Person( "b", "b", 1 ) );
l.add( new Person( "b", "b", 2 ) );
l.add( new Person( "b", "b", 1 ) );
l.add( new Person( "b", "a", 1 ) );
l.add( new Person( "c", "b", 1 ) );
l.add( new Person( "a", "b", 1 ) );
l.add( new Person( "a", "a", 1 ) );
PersonComparator pcAge = new PersonComparator( PersonComparator.COMPARE_BY_AGE, null );
PersonComparator pcLastName = new PersonComparator( PersonComparator.COMPARE_BY_LASTNAME, pcAge );
PersonComparator pcFirstName = new PersonComparator( PersonComparator.COMPARE_BY_FIRSTNAME, pcLastName );
l.sort(pcFirstName);
for ( Person p : l )
System.out.println( p.first_name + " " + p.last_name + " " + p.age );
}
}