• 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

Collections.sort Question

 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an arraylist of type 'DVD' that I want to sort on a particular object. I think I understand the basics but I am having trouble understanding what I should return and the syntax.

I know the above is wrong but it gives you the idea what I want to compare. What I don't quite understand is how to return the sorted DVD instance rather than the title and of course how to define the class for the Comparator declaration. TIA.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
new Comparator(<DVD>()) should be: new Comparator<DVD>()

There is a set of ( and ) that should not be there.

The compare method should not return a DVD; it should return an int (see the API documentation of interface Comparator).
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I just missed that part of the syntax. As for the returned value it was not clear to me from the documentation and examples what 'compareTo' was really returning. Am I correct that it returns an arraylist index of the instance rather than the instance or object itself?
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it does not return an arraylist index.

The API documentation of Comparator.compare() says:

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.


What the actual number is doesn't matter. The only thing that matters is that it's < 0, == 0 or > 0.

The compare() method should only compare the two DVD objects that it gets passed as arguments. It doesn't know anything about the list that is being sorted; it doesn't even know that the DVDs are in a list. So it doesn't know anything about arraylist indices.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. It seems to be working now. I am guessing that the sort uses some kind of sorting algorithm that simply uses the returned value of the comparator to order the list. Thus the number of calls to the comparator can be significant for large lists. Out of curiosity does anyone know the sort algorithm used and its performance for large lists?
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dennis Putnam wrote:Out of curiosity does anyone know the sort algorithm used and its performance for large lists?


That information is in the javadoc as well
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the only job of the Comparator is to compare two objects, and the result will be used for the sorting algorithm. The Comparator doesn't need to know anything about the sorting algorithm - which is good, because it means a different algorithm could be used in a future release and it will still work. It's an example of separation of concerns, an important principle in software engineering.
 
You are HERE! The other map is obviously wrong. Better confirm with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic