0

I'm extremely new to playing with objects so any help is appreciated!I would like to take my objects and compare one field in each object with all my objects.I have created objects with the following:

public class UCFCourse {

public UCFCourse(String courseName, int courseLevel, int courseHoursint, String courseDesc) {
        this.courseName = courseName;
        this.courseHours = courseHoursint;
        this.courseLevel = courseLevel;
        this.courseDesc = courseDesc;
    }

now In a separate file I have this line.I am essentially assigning the values each time I go through a for loop while incrementing x++to assign the fields to a new object courses[x].

courses[x] = new UCFCourse(courseCode, courseLevel, courseHours, replaceString);

I would like to get all these objects into a new file and sort them based off their int courseLevel going from least -> greatest.I have been viewing posts/reading documentation and I am not able to figure this out.

Thanks!

OneU
  • 85
  • 6

1 Answers1

1

You need a custom Comparator. Luckily, in Java 8, it's basically as easy as a method reference:

// Assuming you have a getCourseHours method
Arrays.sort(arr, Comparator.comparing(UCFCourse::getCourseHours));
Mureinik
  • 297,002
  • 52
  • 306
  • 350