-1

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.

Community
  • 1
  • 1
Izzy
  • 6,740
  • 7
  • 40
  • 84
  • You haven't shown any code doing any sorting at all - so of course it's not sorted. Please show the code you've tried in order to sort it... – Jon Skeet Feb 10 '15 at 10:22
  • You need to sort your list. – RealSkeptic Feb 10 '15 at 10:22
  • 2
    `Collections.sort` is the way to go, but you must properly implement `Comparable` in `Person` or create a `Comparator`. Show us what you have done about that. – SJuan76 Feb 10 '15 at 10:22
  • You'll need to show us how you tried sorting. Include the call to Collections.sort, and also the custom comparator you used. – UltraInstinct Feb 10 '15 at 10:22
  • possible duplicate of [Sorting a collection of objects](http://stackoverflow.com/questions/1206073/sorting-a-collection-of-objects) – Joe Feb 10 '15 at 10:23

3 Answers3

1
myList.add(new Person("John Test","9876543211","15/05/1984"));
myList.add(new Person("John Smith","123456789","01/01/1990"));
myList.add(new Person("Some Person","147852369","15/05/1991"));

add like this because arrayList use index to get data. 0 is John Smith because you added it first to list . when iterating 0 gets printed first in the for loop

SabaRish
  • 105
  • 11
0

If I understand you correctly you want to sort by date of birth, right (not "descend")? You need to make your class implement Comparable interface to be able to sort (or pass an implementation of a Comparator to sort method).

Option 1:

public class Person implements Comparable<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;
    }

    @Override
    public int compareTo(Person person) {
        return Dob.compareTo(person.getDob()); // you really want to compare by who is younger here right? whatever it is, put it here instead of String compare
    }

    public String getDob() {
        return Dob;
    }

    public String toString()
    {
        return this.Name + " " + this.Ni + " " + this.Dob;
    }
}

Then you can invoke Collections.sort(myList);

Alternative, use Collections.sort(myList, comparator) and provide a comparator there. An example with anonymous inner class implementation:

Collections.sort(myList, new Comparator<Person>() {
    @Override
    public int compare(Person p1, Person p2) {
        return 0; // do your comparson here
    }
});
theadam
  • 3,961
  • 4
  • 25
  • 41
0
public class PersonAgeComparator implements Comparator<Person>
{
    public int compare(Person p1, Person p2)
    {
        return p1.getAge() - p2.getAge();
    }
}

To invoke the sort you would use:

Collections.sort(people, new PersonAgeComparator());

You will find below link usefull.

https://himanshugpt.wordpress.com/2010/09/10/sorting-list-in-java/

Hope this helps :)

Krutik Jayswal
  • 3,165
  • 1
  • 15
  • 38