Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8199601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:12:15+00:00 2026-06-07T06:12:15+00:00

Here is my class: public static class __9_7_Person implements Comparator<__9_7_Person> { private int height;

  • 0

Here is my class:

public static class __9_7_Person implements Comparator<__9_7_Person> {
    private int height;
    private int weight;
    public __9_7_Person(int height, int weight) {
        this.height = height;
        this.weight = weight;
    }
    public int compare(__9_7_Person p1, __9_7_Person p2) {
        if (p1.height != p2.height) {
            return p1.height - p2.height;
        }
        else {
            return p1.weight - p2.weight;
        }
    }
}

I then created an array like this:

__9_7_Person p[] = {new __9_7_Person(60, 100),
                    new __9_7_Person(70, 150),
                    new __9_7_Person(56, 90),
                    new __9_7_Person(75, 190),
                    new __9_7_Person(60, 95),
                    new __9_7_Person(68, 110),
};

But got exception when I called Arrays.sort(p): “Exception in thread “main” java.lang.ClassCastException: ch_9$__9_7_Person cannot be cast to java.lang.Comparable”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:12:17+00:00Added an answer on June 7, 2026 at 6:12 am

    You should implement Comparable for a natural ordering, in which case you don’t need to pass a separator comparator into Arrays.sort. Or you could implement Comparator<__9_7_Person> (probably in a separate class, e.g. HeightWeightPersonComparator) and call:

    Arrays.sort(p, new HeightWeightPersonComparator());
    

    It’s important to understand the difference between Comparable and Comparator. A Comparable implementation says “I know how to compare myself to another object of an appropriate type” – where a Comparator implementation says “I know how to compare two objects of appropriate types”.

    Obviously any type can only implement Comparable once (within reason), whereas there can be any number of Comparator implementations. Using a separate Comparator is more flexible unless there’s an “obvious” comparison you should use. If you don’t specify a Comparator, Arrays.sort will assume that each of the elements in the array can compare itself with other elements of the array, i.e. they implement Comparable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the code: class qual { public static int fibonacci(int n) { if (n
Here is the code: class Time { public static void printTime (int hour, int
here is the code (java): class prime { public static boolean prime (int a,
I have a few classes as shown here public class TrueFalseQuestion implements Question{ static{
Here is the helper; public class NHibernateHelper { private static ISessionFactory _sessionFactory; private static
Here is my code: public class Test { static { main(null); } public static
Here is my code: import javax.swing.*; import java.awt.*; public class PanelModel { public static
Here is the class: functions.php class buildPage { public function Set($var,$val){ $this->set->$var = $val;
Here is my code: public class Main { public static void main(String[] args) {
Here is my class: public class Command { [XmlArray(IsNullable = true)] public List<Parameter> To

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.