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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:13:31+00:00 2026-06-17T16:13:31+00:00

How do I use a public int compareTo method to compare two objects? I

  • 0

How do I use a public int compareTo method to compare two objects? I need to compare the values within the object to the values of another object to test for greater/less than. So comparing a in 2 objects and b in two objects respectfully.

Test x1 = new Test(9999,9999);
Test x2 = new Test(0,0);

public class Test{
    private int a;
    private int b;

    public Test(){
        a = 0;
        b = 0;
    }


    public Test(int a, int b){
        this.a = a;
        this.b = b;
    }
}
  • 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-17T16:13:31+00:00Added an answer on June 17, 2026 at 4:13 pm

    Implements Compareable<T> interface to class Test. Compareable interface is having a single method comapreTo which accepts the object to be compared.

    class Test implements Compareable<Test>{
        ...
        @Override
        public int compareTo(Test test) {
            // write logic for compare 
            //a negative integer, zero, or a positive integer as 
            //this object is less than, equal to, or greater than the specified object
            return 0;
        }
    }
    
    class Main{
        public static void main(String[] args){
            Test x1 = new Test(9999,9999);
            Test x2 = new Test(0,0);
            int x3 = x1.compareTo(x2);
        }
    }
    

    This interface actually imposes a total ordering on the objects in collection of each class that implements it. This ordering is referred to as the class’s natural ordering, and the class’s compareTo method is referred to as its natural comparison method. Which helps Collection of Object to sort in certain order.

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

Sidebar

Related Questions

Simple. If I use: public void Add(params int[] values) Then I can use this
What I want to do is use the compareTo() method to compare removedItemFromList1 and
I've found that java.lang.Integer implementation of compareTo method looks as follows: public int compareTo(Integer
I want to compare two Lists in test. I have this code @Test public
Please consider the following simple use case: public class Foo { public virtual int
I'm trying to use Gson with an interface: public interface Photo { public int
Use case: class A { static int s_common; public: static int getCommon () const
In a .Net web application I use the public DataRow[] Select(string filterExpression) method in
I'm trying to compare custom type in two List<T> and use the Intersect /
I use this method called SearchConsequences to iterate through List<ValuesEO> of objects and perform

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.