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 6607673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:31:42+00:00 2026-05-25T19:31:42+00:00

Here is my Code class ComparableTest { public static void main(String[] args) { BOX[]

  • 0

Here is my Code

class ComparableTest
{
    public static void main(String[] args)
    {
        BOX[] box = new BOX[5];
        box[0] = new BOX(10,8,6); 
        box[1] = new BOX(5,10,5);
        box[2] = new BOX(8,8,8);
        box[3] = new BOX(10,20,30);
        box[4] = new BOX(1,2,3);
        Arrays.sort(box);
        for(int i=0;i<box.length;i++)
            System.out.println(box[i]);
    }
}

Also i have a class BOX that implements Comparable.
Now i have a few question that i would like you all to help me out with.

1.Are the methods declared in comparable interface,system defined, as in can i have any method in the comparable, or it has to be compareTo only?

2.I did not provide the implementation of Arrays.sort method, how does it sort my elements then?

3.When i use Comparator instead of comparable, then I use:

class comparatorTest
{
    public static void main(String args[])
    {
        Student[] students = new Student[5];
        Student[0] = new Student(“John”,”2000A1Ps234”,23,”Pilani”);
        Student[1] = new Student(“Meera”,”2001A1Ps234”,23,”Pilani”);
        Student[2] = new Student(“Kamal”,”2001A1Ps344”,23,”Pilani”);
        Student[3] = new Student(“Ram”,”2000A2Ps644”,23,”Pilani”);
        Student[4] = new Student(“Sham”,”2000A7Ps543”,23,”Pilani”);    
        // Sort By Name
        Comparator c1 = new studentbyname();
        Arrays.sort(students,c1);
        for(int i=0;i<students.length;i++)
            System.out.println(students[i]);
    }
}

//In the above code, studentbyname implements comparator, but box stil implements comparable .i.e

class studentbyname implements comparator
{
    public int compare(Object o1,Object o2)
    {  
        Student s1 = (Student) o1;
        Student s2 = (Student) o2;
        return s1.getName().compareTo(s2.getName());
    }
}

Now i am doing Arrays.sort(students,c1), why am i passing c1 now?

  • 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-05-25T19:31:43+00:00Added an answer on May 25, 2026 at 7:31 pm
    1. In order to meet the Comparable contract, you must have at least the compareTo method. You may have as many addition methods in your class as you would like.

    2. It sorts the elements in the natural order based on the Comparable interface. So sort is calling compareTo between the elements to see in which order to place them.

    3. Providing a Comparator to the sort method allows sort to order elements that either (a) don’t implement Comparable or (b) where you want to order them in some other order than the “natural order” as defined by the class’s Comparable implementation.

    When you pass a Comparator to sort, it calls the Comparator’s compare method rather than the elements’ compareTo method (if implemented).

    see What is an interface

    see Comparator

    see Comparable

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

Sidebar

Related Questions

Here is the code: class Test { public static void main (String[] args) throws
Here is the code: class Time { public static void printTime (int hour, int
Here's the code: class qual { public static int fibonacci(int n) { if (n
here is the code (java): class prime { public static boolean prime (int a,
Here is the code class Program { public static TraceSource TS = new TraceSource(myTraceSrc,
Here is the code: class Fibonacci { static final int MIN_INDEX = 1; public
Here is the code: class Fibonacci { static final int MIN_INDEX = 1; public
Here's my code: class Ramka extends JFrame { public static final int SZEROKOSC =
Here's my code: class FinallyDemo { static void myMethod(int n) throws Exception{ try {
Here is my code: class Soldier { public: Soldier(const string &name, const Gun &gun);

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.