Java Comparator with Explanation

Java Comparator with Explanation

Hello Friends, How are you? Today I am going to solve the HackerRank Comparator Problem with a very easy explanation. In this article, you will get more than one approach to solve this problem. So let’s start- {tocify} $title={Table of Contents} 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 for you in your editor. It has 2 fields: a name String and a  score 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 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. Input from stdin is handled by the locked stub code in the Solution class. The first line contains an integer, n, denoting the number of players. Each of the n subsequent lines contains a player’s name and score, respectively.

  • 0
No Comments

Sorry, the comment form is closed at this time.