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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:07:56+00:00 2026-06-04T14:07:56+00:00

For natural ordering , I was using comparable and I found as soon as

  • 0

For natural ordering , I was using comparable and I found as soon as I use the method

  public int compareTo(T o1) {      
        System.err.println("this "+this.empID+" that "+((Employee<T>)o1).empID);
        return this.empID - ((Employee<T>)o1).empID;
    }

It works fine, but the this.empID is coming with some logic and that I am not able to figure out. So what is the logic behind this.empID values and what is iteration , is it like on calling sort method , internally the algo is taking care that part (sorting algorithm)

For Example :

on trying to print this.empID and that the output is

this 1 that 5
this 6 that 1
this 6 that 5
this 3 that 5
this 3 that 1
this 7 that 5
this 7 that 6
this 4 that 5
this 4 that 3
this 8 that 5
this 8 that 7
this 2 that 5
this 2 that 3
this 2 that 1

From where the value of this is coming , or what is the iteration logic, is it because of Sorting algorithm.

  • 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-04T14:07:58+00:00Added an answer on June 4, 2026 at 2:07 pm

    If your question is about what code is calling your compareTo method, you’re right that it will generally be called as part of a sorting algorithm. For example, if you insert an Employee a into a TreeSet which contains Employee b(say), you may see calls like a.compareTo(b) or b.compareTo(a), depending on the exact sorting algorithm. In the first case, this is a, and in the second case this is b and that is a.

    To see which code is calling compareTo(), you can set a breakpoint in that method and use a debugger. Alternatively, a quick and crude technique to see the stack traces of the calls is to add the following to compareTo()

    Exception e = new Exception(); // This prepares a call stack
    e.printStackTrace(); // This prints it
    

    Obviously, this kind of debug output is very costly in performance terms, as well as verbose, so it should not be left in production code.

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

Sidebar

Related Questions

I happen to come across many statements like comparable is used when natural ordering
My SSCE: public class ComparableItem implements Comparable<ComparableItem> { private final int itemNo; public ComparableItem(final
I found the following Generating natural schedule for a sports league That generates the
I have a nested associative array like this: $inputTypes= array( natural => array( text,
When we use Natural Join, we are joining the tables when both table have
Suppose I have a table with some natural grouping and ordering, for example records
It would seem natural to me that HTML would support this: <div id=comment1> <div
I am using the --natural option when dumping one of my model into a
In Java, the class String implements Comparable, which means there's a total ordering on
I have the following code: private SetMultimap<String, Dynamic> dynamicFields = TreeMultimap.create(Ordering.natural(), new Comparator<Dynamic>() {

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.