Skip to content

Instantly share code, notes, and snippets.

@namthatman
Created June 22, 2019 08:00
Show Gist options
  • Save namthatman/d9cfe5efbbe17f4c611b674706effb1c to your computer and use it in GitHub Desktop.
Save namthatman/d9cfe5efbbe17f4c611b674706effb1c to your computer and use it in GitHub Desktop.
import java.util.*;
class Player {
String name;
int score;
Player(String name, int score) {
this.name = name;
this.score = score;
}
}
class Checker implements Comparator<Player> {
// complete this method
public int compare(Player a, Player b) {
// compare scores of a and b in DESCENDING order
if (a.score < b.score)
return 1;
else if (a.score > b.score)
return -1;
else {//in case scores are equal
//compare names of a and b in ASCENDING order
int minLength = Math.min(a.name.length(), b.name.length());
for (int i=0; i < minLength; i++) {
//compare in common string length
int aChar = Character.getNumericValue(a.name.charAt(i));
int bChar = Character.getNumericValue(b.name.charAt(i));
if (aChar < bChar)
return -1;
else if (aChar > bChar)
return 1;
else
continue;
}
//if the code runs to HERE without return,
//eg. [bb, 20] and [bbb, 20]
//then compare string lengths for decision
if (a.name.length() < b.name.length())
return -1;
else if (a.name.length() > b.name.length())
return 1;
else
return 0;
}
}
}
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
Player[] player = new Player[n];
Checker checker = new Checker();
for(int i = 0; i < n; i++){
player[i] = new Player(scan.next(), scan.nextInt());
}
scan.close();
Arrays.sort(player, checker);
for(int i = 0; i < player.length; i++){
System.out.printf("%s %s\n", player[i].name, player[i].score);
}
}
}
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 n Player objects, write a comparator that sorts them in order of decreasing score. If 2 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, 1 if a>b, and 0 if a=b.
For example, given n = 3 Player objects with Player.name, Player.score values of
data = [Smith,20],[Jones,15],[Jones,20], we want to sort the list as
data_sorted = [Jones,20],[Smith,20],[Jones,15].
Function Description
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment