Question: How do I implement a comparator by creating an anonymous inner class that orders objects by their length with type Duration? I have my length
How do I implement a comparator by creating an anonymous inner class that orders objects by their length with type Duration? I have my length converted to minutes.
I have this
private final String title;
private final Duration length;
public Video(String title, Duration length) {
this.title = title;
this.length = length;
}
public String title() {
return this.title;
}
public long minutes() {
return this.length.toMinutes();
}
@Override
public String toString() {
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
String stringFormat = "\"%s\", %d minutes long";
Object[] args = {
this.title,
this.minutes(),
};
return String.format(stringFormat, args);
}
// @see Duration#compareTo(Duration)
public static final ComparatorLENGTH_ORDER = ...;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
