Reputation: 18612
I need implement sorting for content of file.
Every line of File contain next data: Instructor, Course, Group, Student, Result
And can contain few results for the same student.
for example:
Paul Schwartz,Introduction to Computer Architecture,I1,Ben Dunkin,88
Muhamed Olji,Object Oriented Programming,I4,Mike Brown,73
...
Paul Schwartz,Introduction to Computer Architecture,I1,Ben Dunkin,96
I need to implement for every instructor
Student - Result
(descending order); Student
with higher result from this course. I made reading from file and created class Item
:
class Item {
private String instructor;
private String course;
private String group;
private String student;
private int result;
// and add appropriate public getters/setters
}
In java, we can sort all the elements of this class using one of the two options: class implementing Comparable
interface and overriding the compareTo() method
or using external Comparator
interface with compare() method
. But how can we sort different querys of sorting using these options.
compareTo()
to satisfy this requirements (or better compare()
)?Upvotes: 1
Views: 111
Reputation: 3749
You should let you class implements Comparable, it has a compareTo
method just like String
does.
So your custom comparator could look like this:
public class CustomComparator implements Comparator<Item> {
@Override
public int compare(Item i1, Item i2) {
return i1.compareTo(i2);
}
}
(The compare()
method must return an int
, so you couldn't directly return a boolean
.)
Than you need to overwrite
the CompareTo
Method in your Item
class, where you define what properties should be compared.
Because you want to sort on different options you need mutliple compare methods, and multiple comparators. Than you just need to call them how you need.
Your sorting code would be just about like this:
Collections.sort(YourObejctList, new CustomComparator());
Upvotes: 2
Reputation: 1678
A comparator for Item
would use the following comparison:
public class CustomComparator implements Comparator {
public int compare(Object aObjectOne, Object aObjectTwo) {
Item objectOne = (Item) aObjectOne;
Item objectTwo = (Item) aObjectTwo;
int cmpinstr = objectOne.getInstructor().compareTo(objectTwo.getInstructor());
if (cmpinstr != 0) // different instructor
return cmpinstr;
int cmpcourse = objectOne.getCourse().compareTo(objectTwo.getCourse());
if (cmpcourse != 0) // different course
return cmpcourse;
int cmpgroup = objectOne.getGroup().compareTo(objectTwo.getGroup());
if (cmpgroup != 0) // different group
return cmpgroup;
// sorting by result.
if (objectOne.getResult() < objectTwo.getResult())
return -1; // i1 has lower result than i2
else if (objectOne.getResult() > objectTwo.getResult())
return 1; // i1 has higher result than i2
return 0; // i1 and i2 have the same result
}
}
To get the top five, simply sort and select the five last students from your collection.
If you want to filter data based on group, instructor, course or result, you can loop through the collection with some custom filter.
Upvotes: 2