Ofcourse there are a lot of blogs about custom comparators on internet but what I find is most of them don't talk about the precautions which one must take while using them in solving questions. I encountered quite a bit difficulty in using it to solve this problem. Do give it a try!!
So lets start with the very basic syntax of writing a custom comparator:
Your code here...