How do you design a comparTo method to match up and isolote certain strings?

32 Views Asked by At

https://github.com/jkirkish/Assignment-4.git

I am working on a coding project. I have to parse the student-master-list.csv File and separate the data into 3 separate CSV files. Each CSV file should contain a list of students specific to that particular course (i.e. we're grouping the students by course).

For each of the 3 CSV files, you'll need to sort the students by grade in descending order.

The CSV output file names should be called: course1.csv, course2.csv, and course3.csv. I am planning to use Bufferred Writer to create each of the three files.

I am having trouble creating my CompareTo method using Strings. Any idea on how to make compareTo method sort students by grade thru descending order and to contain a list of students specific to that particular course?

1

There are 1 best solutions below

0
idanz On

I'd consider working with grades as integers rather than strings. You can create a class Student to represent a student:

public class Student {

  private String name;
  private String course;
  private int grade;

  public Student(String name, String course, int grade) {
    this.name = name;
    this.course = course;
    this.grade = grade;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
  ...
}

And create a List<Student> students to load the students' objects to.

Then, after splitting the students to the 3 courses, you can use Streams (since Java 8) to sort each students' list:

List<Student> studentsOfSomeCourse = getStudentsOfSomeCourse();
List<Student> studentsOfSomeCourseSorted = studentsOfSomeCourse.stream()
    .sorted(Comparator.comparing(Student::getGrade).reversed())
    .collect(Collectors.toList());