• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

compareTo() and Collections.sort() Solution For Multiple Column Ordering (Ascending) question

 
Ranch Hand
Posts: 229
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am somewhat confused with the compareTo() and Collections.sort() behavior.

I am supposed to sort a column in ascending order using compareTo() & Collections.sort().

My criteria is (if the same number occurs than please sort the next available column).

(1) Document Number
(2) Posting Date
(3) Transaction Date
(4) Transaction Reference Number Comparison

Here's the code (which is executed in a calling method) that implements the Collection.sort() method:



Question(s):

(1) Am I doing the right thing? When a comparison = 0, it returns as -2. Is this correct behavior because I always thought it to be between -1,0,1.

(2) Should I be using the comparator?

Happy programming...
 
Marshal
Posts: 27214
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Dekker wrote:(1) Am I doing the right thing? When a comparison = 0, it returns as -2. Is this correct behavior because I always thought it to be between -1,0,1.

(2) Should I be using the comparator?



You could check the API documentation for the compareTo() method, if you wanted an answer to question (1). It says

Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.



And since -2 is indeed a negative integer, that suggests you aren't doing the wrong thing by returning it.

As for (2): you said you were supposed to use compareTo(). So yes, you're supposed to use a Comparator.
 
James Dekker
Ranch Hand
Posts: 229
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,

Thanks so much for the response!

What I am asking is that if I don't use the comparator() that Collections.sort() is still invoking / calling the compareTo() method in my class, right?

Why is it returning -2?

Peace...
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Dekker wrote:(1) Am I doing the right thing? When a comparison = 0, it returns as -2. Is this correct behavior because I always thought it to be between -1,0,1.


THe return value is never guaranteed to be -1, 0 or 1. All you know is there are three outcomes: smaller than 0, 0 or larger than 0. -1 and 1 are just two specific cases, but -2 will be treated in exactly the same way as -1.

(2) Should I be using the comparator?


You should use compareTo which belongs to Comparable. Comparator's method is called compare. So in this case your class should implement CreditCardTransactionDetail, and Comparator is not needed. (I think Paul mixed up Comparable and Comparator.)
You should only use Comparator if:
1) your class doesn't implement Comparable.
2) you want a different sort order.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic