Tomé
Tomé

Reputation: 1

How to Order this LInkedList?

public LinkedList getOrderOfActivityCompletion() {
    LinkedList<Activity> activityOrder = new LinkedList<>();

    if (!activityList.isEmpty() && !times.isEmpty()) {

        for (Activity a : activityList.values()) {

            activityOrder.add(a);


        }

    } else {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    return activityOrder;
}

So, as I said in the title, I need to order this LinkedList of Activity by a parameter of the classe. what is the best way to do this? Do I need to use 2 fors?

Help is much appreciated!

Upvotes: 0

Views: 26

Answers (1)

Ankur Shanbhag
Ankur Shanbhag

Reputation: 7804

Sort the list of activities based on classes. You might need to write custom Comparator to sort the activities based on classes.

Something like this:

Collections.sort(activityList, new Comparator<Activity>() {

    @Override
    public int compare(Activity a1, Activity a2) {
        return a1.getClass().compareTo(a2.getClass());
    }
});

Note: If classes does not override compareTo(), you need to check which class is smaller and return 1. -1 (if a1.getClass() is smaller)
2. 0 (both are equal)
3. 1 otherwise

Upvotes: 1

Related Questions