I am very stuck with this concept, and cannot seem to get my head around it. I have an assignment for school where I need to make an ArrayList<Student>
with a value for String name
, String address
, and int rollno
.
Creating the ArrayList and populating it is not the issue at all. The problem for me is having to use two Comparator classes, and a selection sort to sort the ArrayList by name, and by rollno. I cannot use the Collections.sort
method in this case.
I am very stuck here, and would really appreciate some help.
Thanks!
Sort.java
import java.util.*;
public class Sort {
public Comparator<Student>sortByRoll;
public Comparator<Student>sortByName;
public static void main(String [] args) {
// Declare ArrayList Student
ArrayList<Student>studentList = new ArrayList<Student>();
// Create Objects using Student Class
Student s1 = new Student("John Smith", 1001, "101 Meadow Ave");
Student s2 = new Student("William James", 2011, "1201 Joney Ave");
Student s3 = new Student("Rocky Ferguson", 3003, "1011 Bleeker Street");
Student s4 = new Student("Kitty Roberts", 9284, "906 Dog Ave");
Student s5 = new Student("Lionel Doe", 2100, "221 Colorado Ave");
Student s6 = new Student("Linus Jim", 5993, "26 Cass Ave");
Student s7 = new Student("Haley Henry", 9019, "20001 Log Blvd");
Student s8 = new Student("Penny Cone", 1009, "11 Cracked Court");
Student s9 = new Student("John Shepherd", 1029, "2010 Gold Street");
Student s10 = new Student("Joshua Old", 8463, "77 Railroad Street");
// Adding Objects to ArrayList studentList
studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
studentList.add(s4);
studentList.add(s5);
studentList.add(s6);
studentList.add(s7);
studentList.add(s8);
studentList.add(s9);
studentList.add(s10);
}
}
Student.java
public class Student extends Sort {
// Variables for this class
public int rollno;
public String name;
public String address;
public String student;
// Constructor for creating a student
Student(String name, int rollno, String address) {
this.name = name;
this.rollno = rollno;
this.address = address;
}
//Getters for name, address, and number
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public int getRollNo() {
return rollno;
}
}
SortByName.java
import java.util.Comparator;
public class sortByName extends Sort implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
// Help Plz
};
sortByNum.java
import java.util.Comparator;
public class sortByNum extends Sort implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
//Help plz
}
};
}
You are not allowed to use Collections.sort(Comparator)
because, as you stated, your assignment is to implement selection sort. I will not go into detail on the algorithm (I suppose this is your exercise), but you can implement the compare
methods in your comparators as follows:
For roll number:
@Override
public int compare(Student o1, Student o2) {
return Integer.compare(o1.getRollNo(), o2.getRollNo());
}
For name:
@Override
public int compare(Student o1, Student o2) {
return o1.getName().compareTo(o2.getName());
}
Having these comparators, you can call this compare(Student, Student)
method to get whether one student is less than (-1), equal to (0) or more than (1) the other student according to the comparator. This is what you'll be using to compare Student objects in your selection sort algorithm.
Edit on request: The sort methods themselves can be put in your main class Sort
as follows:
public void sortOnRollNo(List<Student> list) {
// Sort the list here using the sortByRoll Comparator.
}
public void sortOnStudentName(List<Student> list) {
// Sort the list here using the sortByName Comparator.
}