Norm Radder wrote:The code must us an expression like the following to get an int value that can be used to order the Employee objects:
oneEmployee.compareTo(anotherEmployee)
Norm Radder wrote:Look at the source code for the sort method to see how it works.
Henry Wong wrote:
sekhar kiran wrote:
finally how this sorting occurs?
As already mentioned a few times, the details of the sorting algorithm are not officially documented. You will need to examine the source code.
Henry
Norm Radder wrote:Is your problem solved now? If not, please explain.
Ganish Patil wrote:I have run your and mine program in debug mode. I found strange way of comparing these values with each other. In your program values that are compared is salary so here we have your insertion order of salary 80000, 750000, 120000 and 60000. While debuging in variable window after Arrays.sort(e); I saw values being compared in following fashion in public int compareTo(Employee employee)
this.salary operator employee.salary 75000 - 80000 120000 - 75000 120000 - 80000 60000 - 80000 60000 - 75000
Even in my program I added values in List. Insertion order 10, 6 and 60.While debuging in variable window after Collections.sort(myClassList); I saw value being compared in following fashion in public int compareTo(ComparableWithinClassDemo o).
this.classId operator o.classId 6 - 10 60 - 6 60 - 10
I checked source code by clicking on Collections.sort() method it does cloning of this array of objects and does merge sort. It is quite obscure to understand.I don't think we need to know that much in detail. If anyone know will be great If explains
![]()
Norm Radder wrote:The Arrays class's sort() method uses the compareTo() method with two elements in the list of Employees to determine the sort order. The code probably uses an expression like this to determine the order:
oneEmployeeElement.compareTo(anotherEmployeeElement)
Norm Radder wrote:
so this.salary refers 75000 how?
The compareTo method is located in an instance of the Employee class. this.salary refers to the variable in the same instance of the class. The Employee reference passed to the compareTo() method is the other object that is being compared to this object.
Henry Wong wrote:
sekhar kiran wrote:
negative value indicateds salary is less than employee salary am i right?
so here my doubt is
What initialization of Employee object? This is just an method declaration.
Henry
Norm Radder wrote:
what here happening.
Look at the API doc for the Comparable class's compareTo method. It returns an int value that can be used to order the objects in the list that is being sorted.
It determines if this object is less than, equal to, or greater than the specified object.
The code uses it to order the objects based on the value of the salary variable. If this salary is 500 and the employee's salary is 600, the method would return -100.
What does the API doc say about negative values?
Jesper de Jong wrote:
sekhar kiran wrote:i got error
What is the error?
Error messages contain a lot of useful information about what is wrong. Don't just think "Oh, an error. Now I don't know what to do." At least read and try to understand what the error message means. If you don't know, and you post a question in the forums, then include the error message, because it helps other people to understand what's wrong. If you don't tell us what the error is, you're making it hard for us to help you.
Campbell Ritchie wrote:
No. No.sekhar kiran wrote: . . .
so throw will use instead of try catch ,am i right?when we will use throws ofcourse it is system defined excption,which scenario will use
You use throw to signal that something has gone wrong and your method should not continue. You use throws to signal that your method might suffer a particular exception. Google for Java Tutorials Exceptions and read that.
Campbell Ritchie wrote:No. An interface gives much more flexibility. Both have their uses.