I have a ArrayList that is made up of Last Names and First Names together based on the following class:
package nameSorting;
import javax.swing.JPanel;
public class Person extends JPanel{
private String firstName;
private String lastName;
public void enterLast(String string){
lastName = string;
}
public void enterFirst(String string){
firstName = string;
}
public String firstName(){
return firstName;
}
public String lastName(){
return lastName;
}
@Override
public String toString(){
return firstName + " , " + lastName;
}
}
My declaration of that ArrayList is as Follows (I do this in another class):
private ArrayList<Person> savedNames = new ArrayList<Person>();
I am then trying to do Collections.sort but it says I need a comparator. I tried to look up how to use it but nothing seemed helpful to my situation because they all sorted numbers and I am trying to sort by Characters.
My goal is to sort the ArrayList based on Last Name Alphabetically. So first A-Z and then there is also another option to sort Z-A. Any ideas on how to make this work?