• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Problem using the sort function in Java

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to sort an array of objects of type Name. Name is a class that I defined. When I call the sort function, I need to pass the comparison function to sort. I wrote two
comparison functions. Here they are:



The first one works. Second one does not. The second one does not print: bad input. When I use the second one, I get the following error messages:

Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.ComparableTimSort.mergeHi(ComparableTimSort.java:866)
at java.util.ComparableTimSort.mergeAt(ComparableTimSort.java:483)
at java.util.ComparableTimSort.mergeCollapse(ComparableTimSort.java:406)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:213)
at java.util.Arrays.sort(Arrays.java:1246)
at Test2.main(Test2.java:84)
C:\Users\rsher\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)


Here is how the class Name is defined:


It is a generic class and it is not public. Does this matter?

Thanks,
Bob
 
Rancher
Posts: 4893
38
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does statement on line 15 compare against 1?
 
Saloon Keeper
Posts: 9714
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you provide us with a SSCCE (simple self contained compilable example) that we can compile and run that would demonstrate your issue? Other than Norm's comment, I don't see anything wrong, so I'm wondering about the larger context of your code.
 
Master Rancher
Posts: 4321
59
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For example, what happens if this.value = 42.0 and name.value = 42.5?  Then dTmp = 0.5, but what gets returned?
 
Bob Sherry
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Why does statement on line 15 compare against 1?


I should be comparing with 0. I changed the code to compare with 0 and it now works.
 
Bob Sherry
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Can you provide us with a SSCCE (simple self contained compilable example) that we can compile and run that would demonstrate your issue? Other than Norm's comment, I don't see anything wrong, so I'm wondering about the larger context of your code.


I will if you still want me to (it will no be all that simple) but after making the change suggested by Norm, my problem is fixed.
 
Bob Sherry
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:For example, what happens if this.value = 42.0 and name.value = 42.5?  Then dTmp = 0.5, but what gets returned?


Mike you are right. I have a problem. I changed the if to compare with 0 and now it works.
 
Bartender
Posts: 1327
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would focus on the raised exception: as far as I know, Comparison methods violates its general contract means that your comparator methods violates transitive equality. I find suspect that you are testing for dTmp  > 1 to return +1, shouldn't you test for dTmp > 0  instead?
 
A day job? In an office? My worst nightmare! Comfort me tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic