I want to gain depth information about the comparator

How a comparator actually works and what it actually does?

If you want to sort using inbuilt stl and want customized one, use comparator function.
Go through its documentation for more.