Search code examples
javasortingcomparable

Comparing two private variables stored by custom objects that implements 'Comparable'


I'm trying to take a paragraph and find its 'meaning' by printing out the top Three words. after strip out all the grammar word and white space, I use a Hashmap to count the occurrences of each word. then since I don't know of a better way, I just created my own little custom object to store the word, Key, and occurrence, value, just like the Hashmap, but my teacher suggested implementing Comparable, but I've run into a problem. I get two problems, one shows up when I 'fix' the other. The problem lies in the compareTo function in the Pair class, and the Sort() function in the other class. im getting a 'pair cannot be converted to boolean' when I have the compareTo function returning int. and it not letting me use the compareTo function.

compareTo() also I know that the compareTo should return int to Override, but this is how the code was when I jumped on here to ask for help. also the Class prototype is

    public class Pair implements Comparable<Pair>{
    ///.... variables, getters, setters, etc.

    @Override
    public boolean compareTo(Pair o) {    // ERROR HERE

    boolean z = false;

    if (this.getValue() > o.getValue()) {
        z = true;
    }
    else{
        z = false;
    }
    return false; 
}

and the sort/countwords function that is using the Pair object.

public Pair[] sort(Pair[] arr) {
    int max;
    for (int i = 0; i < arr.length; i++) {
        // Assume first element is min
        max = i;
        for (int j = i + 1; j < arr.length; j++) {
            if (arr[j],compareTo(arr[max])  == true){ //ERROR HERE
                max = j;
            }
        }
        if (max != i) {
            final Pair temp = arr[i];
            arr[i] = arr[max];
            arr[max] = temp;
        }
        System.out.println(arr[i].getKey());// I print the in ascending order
    }
    return arr; 
}

public void countWords() {
    Map<String, Integer> m = new HashMap(); // set up a Hashmap to get the occurance of the words
    for (String a : dataList) {
        Integer freq = m.get(a);
        m.put(a, (freq == null) ? 1 : freq + 1);
    }
    //create and fill array of custom Pair (key, value) objects, so it becomes sortable
    int count = 0;
    Pair[] pairs = new Pair[m.size()];
    for (String key : m.keySet()) {
        Pair pair = new Pair(key, m.get(key));
        pairs[count] = pair;
        count++;
    }
    Pair[] sortedPairs = sort(pairs);
}

Solution

  • compareTo() has to return an int which respects the following:

    return -1 if the current objects value is less than the other, 0 if they are equal and 1 if it's greater.

    Change your method to (assuming getValue() returns an int):

    @Override
    public int compareTo(Pair o) {
           return (this.getValue() < o.getValue()) ? -1 : (this.getValue() > o.getValue()) ? 1 : 0 ;
        }
    

    Note that if getValue() returns other object type you could use that object's default implementation of compareTo(). So if getValue() would return for example a BigDecimal or Integer object, etc, you could just use return this.getValue().compareTo(o.getValue());

    Then, when using the method, change the line

    if (arr[j],compareTo(arr[max])  == true)
    

    to

    if (arr[j].compareTo(arr[max])  == 0 ) //they are equal