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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:44:24+00:00 2026-05-23T08:44:24+00:00

What does the return value inside the Comparator actually mean? For example : class

  • 0

What does the return value inside the Comparator actually mean?

For example :

class TreeSetDemo
{
    public static void main(String arg[])
    {
        TreeSet t=new TreeSet(new MyComparator());
        t.add(new Integer(20));
        t.add(new Integer(10));
        t.add(new Integer(30));
        t.add(new Integer(100));
        System.out.println(t); 
    }    

    class MyComparator implements Comparator 
    {    
        public int compare(Object o1, Object o2) 
        {
            return 0;
        }
    }
}

If the return type is 1 then its actually returning

[20, 10, 30, 100]

If the return type is -1 then its actually returning

[100, 30, 10, 20]

If the return type is 0 then its actually returning

[20]

Please tell me what does this indicate?

  • 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-23T08:44:24+00:00Added an answer on May 23, 2026 at 8:44 am

    The return value (not type, the type is int) tells the caller (the thing sorting the data):

    -1 : o1 < o2
    0 : o1 == o2
    +1 : o1 > o2
    

    If you always return the same value (o, 1, -1) for the comparator, regardless of it’s inputs, then you’re not using it correctly. You need to base the value returned on the values passed in. The idea is that the data structure (or sorter) calls the comparison function any time it needs to order two elements, to find out what order to put them in.

    Its worth noting that the positive/negative integer values (-1, +1) don’t need to be 1, they can be any positive/negative numbers. It’s just common practice to return -1/+1.

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

Sidebar

Related Questions

does anyone know how to return a value from a (sql) database into an
Does String.ToLower() return the same reference (e.g. without allocating any new memory) if all
Why does double.parse (1833.35) return value 1833.349999999999 ?
Why does the select statement below return two different values ? declare @tempDec decimal
The method Concat() does not modify the original value. It returns a new value.
Does any Common Lisp (builtin) function return more than 2 values? I know many
Why does the SQLite C/C++ API return unsigned char * s for text values
dir(re.compile(pattern)) does not return pattern as one of the lists's elements. Namely it returns:
If I want to call Bar() instead of Foo() , does Bar() return me
When I make the same query twice, the second time it does not return

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.