comparator is powerful technique to implement custom sort in java.Before java 8 usual way was to use…