Sorting: Comparator

Last modified date

Problem Description:

Comparators are used to compare two objects. In this challenge, you’ll create a comparator and use it to sort an array. The Player class is provided in the editor below. It has two fields:

  1. name: a string.
  2. score: an integer.

Given an array of Player objects, write a comparator that sorts them in order of decreasing score. If or more players have the same score, sort those players alphabetically ascending by name. To do this, you must create a Checker class that implements the Comparator interface, then write an int compare(Player a, Player b) method implementing the Comparator.compare(T o1, T o2) method. In short, when sorting in ascending order, a comparator function returns -1 if a<b , 0 if a=b , and 1 if a>b.

Declare a Checker class that implements the comparator method as described. It should sort first descending by score, then ascending by name. The code stub reads the input, creates a list of Player objects, uses your method to sort the data, and prints it out properly.

Example
n =3 data = [[Smith, 20], [Jones, 15], [Jones, 20]]

Sort the list as date_sorted = [[Jones, 20], [Smith, 20], [Jones, 15]]. Sort first descending by score, then ascending by name.

Input Format

The first line contains an integer, n, the number of players.
Each of the next lines contains a player’s name and score, a string and an integer.

Constraints

  • 0 <= score <= 1000
  • Two or more players can have the same name.
  • Player names consist of lowercase English alphabetic letters.

Output Format

You are not responsible for printing any output to stdout. Locked stub code in Solution will instantiate a Checker object, use it to sort the Player array, and print each sorted element.

Solution

You are given the following to start with:

class Checker{
public:
  	// complete this method
    static int comparator(Player a, Player b)  {

    }
};

We want to look at comparing the scores of each person as we want to sort first by descending score. Since the scores are integers, we can use the normal > and < operators.

class Checker{
public:
  	// complete this method
    static int comparator(Player a, Player b)  {
         if(a.score > b.score){
             return 1; // a > b
         }
         else if(a.score < b.score){
             return -1; //a < b
         }
         else{
             return 0; //a = b but what if two people have the same name? 
         }
    }
};

Next we need to decide what to do if two people have the same score. We could just return 0 like we do above. However, this would not sort the names alphabetically. As per the problem, we are to sort the names with the same score alphabetically.

We can utilize the strcmp() function to compare the strings alphabetically. This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached. To utilize this function we need to make sure we include the pre-processor directive #include<string.h>.

strcmp() compares a C-string to another C-string. Since the name variable is a standard string we will need to utilize the c.str() function to cast it to a C-string.

Out else statement should look as follows:

         else{
             if( strcmp((a.name).c_str(), (b.name).c_str()) > 0){
                 return -1;  //name b comes first alphabetically.
             }
             else if(strcmp((a.name).c_str(), (b.name).c_str()) < 0){
                 return 1; //name a comes first alphabetically.
             }
             return 0; //both names are the same AND have the same score.
         }

The code altogether:

#include<string.h>
class Checker{
public:
  	// complete this method
    static int comparator(Player a, Player b)  {
         if(a.score > b.score){
             return 1;
         }
         else if(a.score < b.score){
             return -1;
         }
         else{
             if( strcmp((a.name).c_str(), (b.name).c_str()) > 0){
                 return -1;
             }
             else if(strcmp((a.name).c_str(), (b.name).c_str()) < 0){
                 return 1;
             }
             return 0;
         }
    }
};
Share post

Bailey Kramer

I am a software developer in Wisconsin who has a passion for mathematics and learning new things. So far I have spent two years in the industry mainly working for a webcasting startup.