• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

collections.binarySearch() is not working with comparable

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
This is my favorite show. And this is my favorite tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic