Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (10.2k points)

I learned how to use the comparable but I'm having difficulty with the Comparator. I am having a error in my code:

Exception in thread "main" java.lang.ClassCastException: New.People cannot be cast to java.lang.Comparable

 at java.util.Arrays.mergeSort(Unknown Source)

 at java.util.Arrays.sort(Unknown Source)

 at java.util.Collections.sort(Unknown Source)

 at New.TestPeople.main(TestPeople.java:18)

Here is my code:

import java.util.Comparator;

public class People implements Comparator {

   private int id;

   private String info;

   private double price;

   public People(int newid, String newinfo, double newprice) {

       setid(newid);

       setinfo(newinfo);

       setprice(newprice);

   }

   public int getid() {

       return id;

   }

   public void setid(int id) {

       this.id = id;

   }

   public String getinfo() {

       return info;

   }

   public void setinfo(String info) {

       this.info = info;

   }

   public double getprice() {

       return price;

   }

   public void setprice(double price) {

       this.price = price;

   }

   public int compare(Object obj1, Object obj2) {

       Integer p1 = ((People) obj1).getid();

       Integer p2 = ((People) obj2).getid();

       if (p1 > p2) {

           return 1;

       } else if (p1 < p2){

           return -1;

       } else {

           return 0;

       }

    }

}

import java.util.ArrayList;

import java.util.Collections;

public class TestPeople {

    public static void main(String[] args) {

        ArrayList peps = new ArrayList();

        peps.add(new People(123, "M", 14.25));

        peps.add(new People(234, "M", 6.21));

        peps.add(new People(362, "F", 9.23));

        peps.add(new People(111, "M", 65.99));

        peps.add(new People(535, "F", 9.23));

        Collections.sort(peps);

        for (int i = 0; i < peps.size(); i++){

            System.out.println(peps.get(i));

        }

    }

}

I believe it has to do something with the casting in the compare method but I was playing around with it and still could not find the solution

1 Answer

0 votes
by (46k points)

Here's a super short template to do the sorting right away :

Collections.sort(people,new Comparator<Person>(){

   @Override

   public int compare(final Person lhs,Person rhs) {

     //TODO return 1 if rhs should be before lhs 

     //     return -1 if lhs should be before rhs

     //     return 0 otherwise (meaning the order stays the same)

     }

 });

if it's hard to remember, try to just remember that it's similar (in terms of the sign of the number) to:

 lhs-rhs 

That's in case you want to sort in ascending order : from smallest number to largest number.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Feb 17, 2021 in Java by Jake (7k points)
0 votes
1 answer
0 votes
1 answer
asked Nov 13, 2019 in Java by Nigam (4k points)
0 votes
1 answer
asked Nov 20, 2019 in Java by Anvi (10.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...