wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes collections.binarySearch() is not working with comparable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "collections.binarySearch() is not working with comparable" Watch "collections.binarySearch() is not working with comparable" New topic
Author

collections.binarySearch() is not working with comparable

Nakul P. Patel
Greenhorn

Joined: May 31, 2011
Posts: 25
After sorting List in ascending and descending order i want to invoke binarySearch method. But it not working as i have implemented comparable.
I could have done this by implementing comparator but i have such requirement in my project Please guide me.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2247
    
    7

Comparable returns a int -1, 0 or 1. Zero is equals, -1 if input1 is bigger, and 1 if input2 is bigger.

In your code, you should do a < or > comparison instead of the difference.

If you want to use the student name as comparison, you can use the string's compareTo() method.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7554
    
  18

Nakul P. Patel wrote:After sorting List in ascending and descending order i want to invoke binarySearch method. But it not working as i have implemented comparable.

Are you sure about that? It looks to me as though you're just not calling it properly.

It's probably also worth mentioning that if you're using Comparators, you have to invoke it with the same Comparator that was used to sort.

PS: Please UseCodeTags (←click) when you post code. I've added them for you this time - See how much better it looks?

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

K. Tsang wrote:Comparable returns a int -1, 0 or 1. Zero is equals, -1 if input1 is bigger, and 1 if input2 is bigger.

In your code, you should do a < or > comparison instead of the difference.

Not true. The return value is < 0, 0 or > 0. The < 0 doesn't have to be -1, and the > 0 doesn't have to be 1. Unless the subtraction leads to overflow, it's a valid implementation.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Rob Spoor wrote:
K. Tsang wrote:Comparable returns a int -1, 0 or 1. Zero is equals, -1 if input1 is bigger, and 1 if input2 is bigger.

In your code, you should do a < or > comparison instead of the difference.

Not true.

yes , until i write -4 or 4
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

The "not true" was meant for the -1, 0 or 1. I shouldn't have quoted that last sentence, because you should use < and > -- not inside compareTo but for the result of it.
 
 
subject: collections.binarySearch() is not working with comparable
 
Similar Threads
Asking for help with Sort-Program (Arrays)
Regarding java.lang.Comparable.
Sorting
Please explain me this!
Why The Output Is Same...