I'm using a comparator to sort files by modified date. Those files are to be processed for contained data and deleted after. I have to make sure the files are in sequential order before processing them. Below is what I have so far.
public class FileComparator implements Comparator<Path> {
@Override
public int compare(Path o1, Path o2) {
try {
return Files.getLastModifiedTime(o1).compareTo(Files.getLastModifiedTime(o2));
} catch (IOException exception) {
//write to error log
//***
}
}
}
*** This is where I'm stuck. I have to return an int because compare requires it but I don't want to return zero and have false equivalency when it fails.
I tried restructuring the code but then if getLastModifiedTime() fails, o1Modified and o2Modified will be null.
public class FileComparator implements Comparator<Path> {
@Override
public int compare(Path o1, Path o2) {
FileTime o1Modified = null;
FileTime o2Modified = null;
try {
o1Modified = Files.getLastModifiedTime(o1);
o2Modified = Files.getLastModifiedTime(o2);
} catch (IOException exception) {
//write to error log
}
return o1Modified.compareTo(o2Modified);
}
}
Is there any standard way to handle situations like this?