Sort object with different values using comparators

/**
 * Show multiple ways to sort objects with different values. 
 * @author Xuan Ngo
 */
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Collections;
public class SortingUsingComparator
{
 
  public static void main(String[] args)
  {
    /**
     * Create data
     */
    Person a = new Person();
    Person b = new Person();
    Person c = new Person();
    Person d = new Person();
    a.firstname = "chris";  a.lastname = "bourgois";
    b.firstname = "betty";  b.lastname = "smith";
    c.firstname = "daniel"; c.lastname = "lopez";
    d.firstname = "alain";  d.lastname = "gonzalez";
    Person[] aPersons = {a, b, c, d}; // The ARRAY
 
    ArrayList<Person> lPersons = new ArrayList<Person>(); // The LIST
    lPersons.add(a); lPersons.add(b); lPersons.add(c); lPersons.add(d); 
 
    /**
     * First alternative.
     * Sort ARRAY by firstname using FirstnameComparator class.
     */
    System.out.println("Sort array by first name in ascending order:");
    Arrays.sort(aPersons, new FirstnameComparator());
    displayPersonsArray(aPersons);
 
    /**
     * Second alternative.
     * Sort LIST by firstname using FirstnameComparator class.
     */
    System.out.println("Sort list by first name in ascending order:");
    Collections.sort(lPersons, new FirstnameComparator());
    displayPersonsList(lPersons);
 
    /**
     * Third alternative.
     * Sort ARRAY by lastname in descending order with instant creation of comparator.
     */
    System.out.println("Sort by last name in descending order:");
    Arrays.sort(aPersons, new Comparator<Person>()
                  {
                    public int compare(Person p1, Person p2)
                    {
                      return p2.lastname.compareTo(p1.lastname);
                    }                
                  }
                );
    displayPersonsArray(aPersons);
 
 
  }
 
  /**
   * Display person info.
   * @param aPersons
   */
  public static void displayPersonsArray(Person[] aPersons)
  {
    for(int i=0; i<aPersons.length; i++)
    {
      String s = String.format("\t%s. %s %s", i, aPersons[i].firstname, aPersons[i].lastname);
      System.out.println(s);
    }
  }
  /**
   * Display person info.
   * @param lPersons
   */
  public static void displayPersonsList(ArrayList<Person> lPersons)
  {
    for(int i=0; i<lPersons.size(); i++)
    {
      String s = String.format("\t%s. %s %s", i, lPersons.get(i).firstname, lPersons.get(i).lastname);
      System.out.println(s);
    }
  }
}
/**
 * Person.java
 * Simple object that we want to sort.
 */
public class Person
{
  public String firstname, lastname;
}
/**
 * FirstnameComparator.java
 */
import java.util.Comparator;
 
/**
 * Implementation of Comparator class for Person.
 * Sort only by firstname in alphabetic order. 
 */
public class FirstnameComparator implements Comparator<Person>
{
  public int compare(Person p1, Person p2)
  {
    return p1.firstname.compareTo(p2.firstname);
  }
}
AttachmentSize
Package icon SortingUsingComparator.zip1.42 KB