2

I am building a android project where am storing some details which includes date as string,but now I need to sort the list view by date.I am bit confused on how to do it.I am done with everything but need to sort the list by date.

Here is the overview of how am storing data.

I have an arraylist of type Person like

ArrayList<Person> personDetails = new ArrayList<Person>();

Person class consists of several variable but I will limit to three 1)name 2)Record created(date) 3)Address

then add to Person like personDetails.add(Person Object).

Please guide me on how to sort the list by date.

Thanks for your time.

5
  • 1
    Convert the strings to actual dates and sort them as dates. Commented Sep 1, 2014 at 12:51
  • 1
    Make Person implement Comparable and convert the string dates to actual dates and compare them in the compareTo method, you can then sort it using Arrays.sort Commented Sep 1, 2014 at 12:52
  • Any code sample would help. Commented Sep 1, 2014 at 12:53
  • @lear I suggest you do a quick search for how to convert a string to a date and how to compare dates. In the last 19 years, there have been one or two million of these already. Commented Sep 1, 2014 at 12:56
  • Thank you again to all who tried to help and code is working like charm.But can anyone explain me the internal working of this. Commented Sep 1, 2014 at 13:14

3 Answers 3

3

Or do something like that if you don't want to implements Comparable interface in your Person class:

List<Personne> personDetails = new ArrayList<Personne>();
Collections.sort(personDetails, new Comparator<Personne>()
{
  public int compare(Person o1, Person o2) {
    SimpleDateFormat sdf = new SimpleDateFormat("YOUR-DATE-PATTERN");
    Date d1 = sdf.parse(o1.getDateAsString());
    Date d2 = sdf.parse(o2.getDateAsString());
    return d1.compareTo(d2);
  }
});
Sign up to request clarification or add additional context in comments.

Comments

2

Implement Comparable in your Person class:

public class Person implements Comparable<Person> {

    public int compareTo(Person person) {
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
        return sdf.parse(this.dateString).compareTo(sdf.parse(person.dateString));
    }
}

Replace "YYYY-MM-dd" with the format of your date string.

Comments

1

You can implement Comparable in your class Person, and define the order in the compareTo method.

public class Person implements Comparable<Person>{

    public int compareTo(Person comparePerson) {
        //logic here
    }
}

Then just call:

Collections.sort(personDetails);

Good tutorial: http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/

2 Comments

hey can u explain me the internal of this.Once again thank you for the help.
Once you had implemented Comparable, you will be able to sort it the way it was defined inside de method compareTo. In @ToasterR answer, he implemented it, so get that code, it is going to work. Then you will have just to call Collections.sort, and the collection will be sorted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.