Array Sort List of objects based on the variable in the object

I have an array of objects

The objects in the array list are college information called "ModuleInfo" (course, assignments, dateDue )

DateDue formatted to integer YYYYMMDD (from calendar)

I looked at some other ways that people did this, but I can't figure out what I need to do.

Ideally, since I already indicated when creating the list of arrays that it would contain ModuleInfo objects, I could just Collection.sort (moduleDB, ModuleInfo.getDateDue) or something like that. moduleDB - Array List

Any help would be greatly appreciated.

+5
source share
1 answer

Collections.sort(List list) , Comparable.

public class ModuleInfo implements Comparable<ModuleInfo> {

    /* Your implementation */

    public int compareTo(ModuleInfo info) {
        if (this.dateDue < info.dateDue) {
            return -1;
        } else if (this.dateDue > info.dateDue) {
            return 1;
        } else {
            return 0;
        }
    }
}

Collections.sort(moduleDB), moduleDB ArrayList<ModuleInfo>.

p.s. , Comparator .

+5

All Articles