I'm very new to Java and object oriented programming. I'm trying to sort a 2d arrangement of objects. Currently my code reads a CSV file and puts the each line in a new object, and each object has 6 values relating to the file.
Here is my code:
public static void readFile() {
String line = "";
try {
BufferedReader br = new BufferedReader(new FileReader(Main.filepath));
while ((line = br.readLine()) != null) {
String values[] = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
String filmName = values[0];
int filmYear = Integer.parseInt(values[1]);
String filmRating = values[2];
String filmGenre = values[3];
int filmLength = Integer.parseInt(values[4]);
double filmScore = Double.parseDouble(values[5]);
Object[] film = {filmName, filmYear, filmRating, filmGenre, filmLength, filmScore};
fileData.add(film);
}
} catch (Exception ex) {
System.out.println("File Not Found");
}
}
I'm aware that declaring each variable is ultimately unnecessary, however it makes it easier for me to understand.
I want to sort the array by the filmLength
variable using .sort()
but I'm not sure how.