You can use
1) <T> void java.util.Collections.sort(List<T> list) to sort the ArrayList, if your the student objects implements Comparable. or
2) <T> void java.util.Collections.sort(List<T> list, Comparator<? super T> c), if you have Comparator to compare Student Objects.
Write a class that implements Comparator.
This class should have a single method called compare, that returns an int, and takes 2 Object objects as parameters.
Inside this method Cast the Objects to your classes within your list. And write some code that compares the fields you want to sort by.
Basically if Object 1 should come before Object 2 you should return -1.
Check the API of the Interface if you are unsure what methods are required and what they should return.
Then use this static method to sort your list using the given comparator object
Collections.sort(yourList, new YourComparator());
Perhaps not the most efficent way, but it works, and its simple to understand.
This was the best way I could describe what you have to do without just giving you the code
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
4
posted
0
Joshua Barrett wrote: . . . Cast the Objects to your classes within your list. . . .
Surely you write a class which implements the Comparator<Student> interface, then you can dispense with those casts.
Joshua Barrett
Greenhorn
Joined: Sep 08, 2010
Posts: 27
posted
0
Campbell Ritchie wrote:
Joshua Barrett wrote: . . . Cast the Objects to your classes within your list. . . .
Surely you write a class which implements the Comparator<Student> interface, then you can dispense with those casts.
Blimeh, that would do away with the extra class! Never knew that existed. Ya learn something new every day
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
4
posted
0
Joshua Barrett wrote: . . . Ya learn something new every day
That's why I came here in the first place. I have learned lots and lots here.