• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparable and Collections.sort()

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All,

I'm playing around with the sort method in collections and getting some behavior that isn't what I expect.

Inside a simple class that extends Comparable, I have a compareTo that looks like:



When I do a Collections.sort() on a list populated with this type, the results look very unsorted. I know compareTo is being called based on the debug output (below). Also in the debug you can see the previous sort (using a Comparator) which is the input List for the Comparable sort.

######### SORTED LIST (INT) ########
String: Hello Int: -3
String: World Int: -1
String: Hello Int: 2
String: Bar Int: 3
String: Foo Int: 100
String: Hello Int: 300
######### SORTED LIST (INT) ########

######### SORT BASED ON COMPARABLE INTERFACE ########
String compareTo resulted in -15
String compareTo resulted in 15
String compareTo resulted in 6
String compareTo resulted in -4
String compareTo resulted in -2
String: Hello Int: -3
String: World Int: -1
String: Hello Int: 2
String: Bar Int: 3
String: Foo Int: 100
String: Hello Int: 300
######### SORT BASED ON COMPARABLE INTERFACE ########

Any insight would be greatly appreciated.

Shawn
 
Shawn Smith
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never mind, I'm a moron.

Amazing how much != and == look alike after a few hours of coding....
 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic