posted 15 years ago
Not necessarily. You can use it anytime you have objects that you want to compare that you want to specify how the comparison works.
You used the example above of having various Collection1 objects inserted into a List, and having them sort by the numerical value of the internal variable 'c' - a perfectly reasonable usage.
In exactly the same way, you might want to have a class that contains a person's name and country (extremely simplified example):
In such a case, you might want to have sort order determined by lastName then firstName, except when the country entered is one of the countries that has family name first. This could give me a sorted list such as:
Last, First, CountryAbrahams, John, EnglandShen, Fox, ChinaJohanson, Donald, USA
As you can see, I want Mr Fox from China sorted between Mr Abrahams from England and Mr Johanson from USA, even though a "standard" sort based on the name in the "first-name" field does not work.
Another example might be when you want to compare currencies. Which is the greater sum: $ 0.50 USD, or ¥ 30 JPY ? If you must keep the original currency, then you probably want to have a specialized comparator behind the scenes to handle the comparison since you cannot just compare the numbers directly.