-2

I'm trying to write some code and I need to use a comparator, but I have no idea what a comparator is. Could you please explain why we use Comparators and what is comparable?

And also am I doing this correctly? I tried to use implements Comparator<> on the top, but it was not working.

Here is my code.

 import java.util.Comparator;
 public class MedalCount {
 String name;
 int goldMedals;
 int silverMedals;
 int bronzeMedals;
 int totalMedals;


/** 
 * Creates a new MedalCount instance.
 * @param name is the name of the nation
 * @param nGoldMedals is the number of gold medals
 * @param nSilverMedals is the number of silver medals
 * @param nBronzeMedals is the number of bronze medals
 */
public MedalCount(String name, int nGoldMedals, int nSilverMedals,
                  int nBronzeMedals) {
    this.name = name;
    this.goldMedals = nGoldMedals;
    this.silverMedals = nSilverMedals;
    this.bronzeMedals = nBronzeMedals;
    totalMedals = nGoldMedals + nSilverMedals + nBronzeMedals;
}

/** Return a string describing the medal count information for the country */
public String toString() {
    return name + ":\t" + goldMedals + ",\t" + silverMedals + ",\t" 
        + bronzeMedals + ",\t"+ totalMedals;
}

/** 
 * Compare instance of medal count with another. 
 * Nations compared based on number of total medals (descending order).
 */
// TODO: create comparator
public static final Comparator<MedalCount> COMPARATOR = new Comparator<MedalCount>() {
    @Override
    public int compare(MedalCount totalMedals1, MedalCount TotalMedals2) {
        if (totalMedals1.totalMedals >= totalMedals1.totalMedals) {
            return 1;
        } else
            return 0;


    }
}




/** Comparator for sorting by nation name alphabetically */
// TODO: create comparator


/** Comparator for sorting by gold medal count (descending order) */
// TODO: create comparator 

}

Jodast
  • 1,279
  • 2
  • 18
  • 33
pigpug pee
  • 35
  • 1
  • 5

3 Answers3

2

It's used to do equality checking. Let's say you have a list of objects belonging to the same class. The class has 3 properties you can say they are equal if all 3 bits of data are the same. But you could also say second property has higher precedence over 3rd and 1st property comes last. So it's how operators like <, <= = =>, >, <> behave when comparing a list of said objects. So you can then do things like sort a list of objects for that class.

Additional background is available from the Comparator javadoc.

JGFMK
  • 8,425
  • 4
  • 58
  • 92
1

The bug in your code is on this line: if (totalMedals1.totalMedals >= totalMedals1.totalMedals)

You need to compare BOTH objects, you have totalMedals1 twice and never use totalMedals2.

Freiheit
  • 8,408
  • 6
  • 59
  • 101
1

your condition is wrong in compare method both object is same. change totalMedals1 to totalMedals2

when you sort your objects these interface used, like example you want use Collections.sort(ListOfMedal) based on this logic your object will get sorted

Comparable provides single sorting sequence and Comparator provides you multiple sorting sequences, try with below code

if (totalMedals1.totalMedals >= totalMedals2.totalMedals) return 1;
if (totalMedals1.totalMedals <= totalMedals2.totalMedals) return -1;
else return 0;
Community
  • 1
  • 1