Comparator classes with fields

MercyDude :

In a project where one uses a class which implements a Comparator Interface, in order to compare between some comparable objects I've noticed that I can design the class which implements the Comparator<> interface with fields, and then Override the compare(...) function and use the class's fields for the compare function logic.

so I'll have to call the sort function something like this:

Collections.sort(someArrayList, new SortClass(argument1, argument2));

My questions are:

  • How common is doing something like this?

  • Is it considered good design?

  • Assuming I get a user input which should change the logic of the comparison between some objects, building a new wrapper class (with the given parameters) would it be considered as a better solution for that matter?

As requested my SortClass is (I generalized it in the above section but here is my real sorting class):

public class SortHouses implements Comparator<Hotel> {

    /** if house1 should be before house2 */
    private static final int GT = -1;

    /** if house1 should be after house2 */
    private static final int LT = 1;

    private double latitude;
    private double longitude;

    public SortHouses(double latitude, double longitude){
        this.latitude = latitude;
        this.longitude = longitude;
    }

    @Override
    public int compare(House house1, House house2) {
        double distHouse1 = Math.sqrt((Math.pow((house1.getLatitude() - latitude), 2) +
                                 Math.pow((house1.getLongitude() - longitude), 2)));
        double distHouse2 = Math.sqrt((Math.pow((house2.getLatitude() - latitude), 2) +
                Math.pow((house2.getLongitude() - longitude), 2)));

        if (distHouse1 < distHouse2){
            return GT;
        }
        if (distHose1 > distHouse2) {
            return LT;
        }
        if (house1.getNum() > house2.getNum()){
           return GT;
        }
        return LT;
    }
}
Michael :

How common doing something like this is?

A parameterised Comparator? Not very common. Usually things are sorted with regards to their own properties.

Is it considered a good design?

Yes, if you want to sort locations by distance to a reference location, then using a parameterised Comparator seems like a good way to accomplish this.

However, I can see one thing I don't like. Your SortHotelsByProximity is actually doing a "secret" comparison with POI (points of interest?) in case of a draw.

It would be clearer, and give you more flexibility later, if you were to move this logic into a second Comparator: SortHotelsByPOI. You can combine Comparators together to account for draws with the method thenComparing, which would look something like this:

hotels.sort(new SortHotelsByProximity().thenComparing(new SortHotelsByPOI()))

Assuming I get a user input which should change the logic of the comparison between some objects, building a new wrapper class (with the given parameters) would be considered as a better solution for that matter?

I don't know what you mean by 'wrapper class', but building a comparator dynamically based upon user input is fine, if that's what you're asking.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=98359&siteId=1
Recommended