Skip to main content

Posts

Showing posts with the label Comparator Interface

How to Sort object property with null values using Java 8 Lambda expression

Sort custom object property with null values using Java 8 lambda expression What is lambda expression? The expression through which we can represent an anonymous function. learn more about lambda expression : Lambda Expression in Java 8 with examples   Lets see how we can sort custom object property without and with lambda expression. Example 1 : Sort custom object without lambda expression using Collections.sort() Programming.java public class Programming {     private int id;     private String name;          public Programming(int id, String name) {         this.id = id;         this.name = name;     }     public int getId() {         return id;     }     public void setId(int id) {         this.id = id;     }    ...

Java Sort Hackerrank Solution | Comparator with Three Properties

Sort Java List using Comparator Interface for Three Object Values Problem Description :- You are given a list of student information: ID, FirstName, and CGPA. Your task is to rearrange them according to their CGPA in decreasing order. If two student have the same CGPA, then arrange them according to their first name in alphabetical order. If those two students also have the same first name, then order them according to their ID. No two students have the same ID. Hint : You can use comparators to sort a list of objects. See the oracle docs to learn about comparators. Input Format The first line of input contains an integer N, representing the total number of students. The next N lines contains a list of student information in the following structure:      ID  Name CGPA Read full description on HackerRank :- https://www.hackerrank.com/challenges/java-sort/problem Solution 1 :- Sort Java list using Inner class (Comparator) with Three Object properties In this p...

Java Comparator Hackerrank Solution with Explanation | Programming Blog

Comparing Objects with Comparator in Java | Compare and Sort two Objects in Java 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 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 Format 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. Read full description on Hackerrank Website :- https://www....