I have a simple ArrayList
and it doesn't seem to print the way I want it to. I have a class name Person
which is as follows:
public class Person {
public String Name;
public String Ni;
public String Dob;
public Person(String name, String ni, String dob){
this.Name = name;
this.Ni = ni;
this.Dob = dob;
}
public String toString()
{
return this.Name + " " + this.Ni + " " + this.Dob;
}
}
And then to print the list I simply do
public static void main(String []args){
ArrayList<Person> myList = new ArrayList();
myList.add(new Person("John Smith","123456789","01/01/1990"));
myList.add(new Person("John Test","9876543211","15/05/1984"));
myList.add(new Person("Some Person","147852369","15/05/1991"));
for(Person person : myList)
{
System.out.println(person);
}
}
It prints the list as expected however I'm trying to descend by Dob
but I can't seem to work out how to achieve this. I've tried Collections.sort
after implementing my Person
class but still have the same issue.
Actual Result:
John Smith 123456789 01/01/1990
John Test 9876543211 15/05/1984
Some Person 147852369 15/05/1991
Desired Reasult:
John Test 9876543211 15/05/1984
John Smith 123456789 01/01/1990
Some Person 147852369 15/05/1991
Would highly appreciate it if someone can help me on this issue.