-1

I have two Model classes that I am adding to ArrayList items and both of the model classes have a getDate() and I want to sort the list by date. I have been going at it for a while but can't seem to get it.

    ArrayList<Object> items = new ArrayList<>();
    ArrayList<LiftDate> liftDates = LiftDateHandler.get(getActivity()).getDate();
    ArrayList<OneRepMax> oneRepMaxes = OneRepMaxHandler.get(getActivity()).getORM();
    items.addAll(liftDates);
    items.addAll(oneRepMaxes);

Both LiftDate.class and OneRepMax.class contain:

public Date getDate() {
    return mDate;
}

How do I sort the ArrayList items based on the two Objects getDate()?

0

1 Answer 1

2

Step #1: Define a common interface that LiftDate and OneRepMax both implement. This interface should include getDate(). I'll use Snicklefritz as the name of this interface in this answer; you should choose something more meaningful for your app.

Step #2: Have items be an ArrayList<Snicklefritz>.

Step #3: Implement a Comparator<Snicklefritz> that performs your date comparison using getDate().

Step #4: Use Collections.sort() to sort the ArrayList using your Comparator.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.