Home > OS >  How to use CompareTo() Lexicographically?
How to use CompareTo() Lexicographically?

Time:10-12

Good morning/night, I am a beginner in java. I'm making a class for a program where people can set locations with various variables (state, country, latitude, etc) as a way to practice using multiple classes for a project. In this class (called Location), I'm forced to use compareTo() in a way I haven't used it before since I'm implementing the Comparable class. I know compareTo() is used to compare strings by seeing if they're the same or different, but this time I'm comparing two objects within a method. How would I sort these objects within the method and still return an int?

This is my Location class at the moment. I would have only posted the current method, but based on other posts in the forum, it's good to show the a good chunk of the class.

public class Location implements Comparable<Location> {

    //Done for now
    private String state;
    private String county;
    private double latitude;
    private double longitude;
    private int elevation;
   
    public Location(String state, String county) throws IllegalArgumentException {
       this.state = state;
       this.county = county;
    }
    //...
    //various getters and setters for mentioned variables...
    //...

    public int compareTo(Location a){ //must compare objs and return an int
       int result = this.compareTo(a);
 
       if (result == 0){
        // the equal
            return result;
        } else if (result < 0){
            //not equal, one would be higher than the other
            return result;
        } else{
            //Same as comment above
            return result;
        }
    }


I have been looking in forums and some java focused websites for help, but they only show it in the way I'm used to using the method (think s1.comparesTo(s2)). I haven't stumbled upon any posts that involves implementing Comparable.

CodePudding user response:

Why not cheat and use the Comparator builder?

public class Location implements Comparable<Location> {
    private static final Comparator<Location> comparator = Comparator.comparing((Location l) -> l.state)
        .thenComparing(l -> l.county)
        .thenComparingDouble(l -> l.latitude)
        ...;

    public int compareTo(Location a) {
        return comparator.compare(this, a);
    }
}

The alternative is manually comparing a field, return the result if it's not zero, otherwise move onto the next field and repeat.

CodePudding user response:

You could || these together

if (this.state.compareTo (a.getState()) < 0 || 
    this.county.compareTo (a.getCounty()) < 0 || ....) return -1;

then compareTo for greater

if (this.state.compareTo (a.getState()) > 0 || 
    this.county.compareTo (a.getCounty()) > 0 || ....) return 1;

if not then all is hunky dory.

CodePudding user response:

I'm also a beginner for java, so please forgive me if my explanation is in question. The interface Comparable is use for sorting, so firstly you must tell us what do you want to sort by. For example, you can sort your locations by elevation by this:

public int compareTo(Location a){ //must compare objs and return an int
    if(elevation < a.getElevation()){
        return -1;
    }
    else if(elevation == a.getElevation()){
        return 0;
    }
    else
        return 1;
}

Hope my answer helpful and wish you successful.

  • Related