I m new in java so i want to tthe comlete solution of this code

i m new in java so i want to tthe comlete solution of this code that how to implement comparators in java

Even if you are new in java, you need to try to write the code on your own. Here I can help you in concepts to make your writing of code a bit easier.

  1. if you have learnt about sorting algorithm, you know that merge and quick sort are best as their TC is O(nlogn). in java, we have these sorting algorithms implemented. we don’t need to write them from scratch. we just need to use Collections.sort() method by providing it required parameters.
  2. a simple example of Collections.sort() to sort a list of Strings.
    import java.util.*;

public class Collectionsorting
{
public static void main(String[] args)
{
// Create a list of strings
ArrayList al = new ArrayList();
al.add(“Geeks For Geeks”);
al.add(“Friends”);
al.add(“Dear”);
al.add(“Is”);
al.add(“Superb”);

    /* Collections.sort method is sorting the 
    elements of ArrayList in ascending order. */
    Collections.sort(al); 

    // Let us print the sorted list 
    System.out.println("List after the use of" + 
                       " Collection.sort() :\n" + al); 
} 

}
here you just need to pass string list to Collections.sort().

  1. To sort a list, java must know how to compare two elements of a list. In case of primitive types(and String), the java compiler knows how to compare these elements But to compare user defined data types, its user responsibility to let the compiler know how to compare these data types. this is done using Comparator in java.
    Java Comparator interface is used to order the objects of a user-defined class.

  2. there is another version of Collections.sort() which takes two parameters. the second parameter tells the compiler(or better say interpreter), how to compare the objects of user defined class.

  3. Lets see an example of Comparator.
    the prototype of sort method is Collections.sort(List list, Comparator c). so it needs a Comparator object and since Comparator is an interface, we need to define a class that implements this interface, then we can pass the object of that class.
    we have a user defined class student and another class Sortbyroll which implements Comparator. analyze the below code carefully:

import java.util.;
import java.lang.
;
import java.io.*;

// A class to represent a student.
class Student
{
int rollno;
String name, address;

// Constructor 
public Student(int rollno, String name, 
                           String address) 
{ 
    this.rollno = rollno; 
    this.name = name; 
    this.address = address; 
} 

// Used to print student details in main() 
public String toString() 
{ 
    return this.rollno + " " + this.name + 
                       " " + this.address; 
} 

}

class Sortbyroll implements Comparator
{
// Used for sorting in ascending order of
// roll number
public int compare(Student a, Student b)
{
return a.rollno - b.rollno;
}
}

// Driver class
class Main
{
public static void main (String[] args)
{
ArrayList ar = new ArrayList();
ar.add(new Student(111, “bbbb”, “london”));
ar.add(new Student(131, “aaaa”, “nyc”));
ar.add(new Student(121, “cccc”, “jaipur”));

    System.out.println("Unsorted"); 
    for (int i=0; i<ar.size(); i++) 
        System.out.println(ar.get(i)); 

    Collections.sort(ar, new Sortbyroll()); 

    System.out.println("\nSorted by rollno"); 
    for (int i=0; i<ar.size(); i++) 
        System.out.println(ar.get(i)); 
} 

}

Note:
this program is sorting a list of students based on their roll numbers.
Comparator is part of syntax to tell the compiler that this Comparator is of type Student.
Most imp part of the program is compare() method of Sortbyroll class. as Comparator is of Student type, both arguments of compare() method are also of type Student.
if compare() returns +ve value --> means first object is greater.
if it return 0 --> both objects equal.
if it returns -ve value --> second object is greater.

  1. so you need to modify compare() method in such a way that it sort the objects in the way as question demands.

Thanks

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.