Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections.sort() on Array List

 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Below is code I created based on Sierra/Bates Chapter 7. It compiles and runs; I have a related question. See below.



This following is output:

[2001 sci-fi ??
, Caddy Shack comedy Murray, Bill
, Donnie Darko sci-fi Gyllenhall, Jake
, Lost in Translation comedy Murray, Bill
, Patriot Games action Ford, Harrison
, Raiders of the Lost Ark action Ford, Harrison
, Star Wars sci-fi Ford, Harrison
]



Is there another way this code can compare dvdList without having DVDInfo extend Comparable? Sierra/Bates mentions:

Returning to our earlier example for class DVDInfo, we can take the easy way out and use the String class’s implementation of compareTo():



Please clarify what is the un-easy way to compare dvdList?

 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at java.util.Comparator
 
Alpesh Padra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can implement sorting by 2 way

1. Comparable
2. Comparator with compare(T o1, T o2) method


 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this you want? Check and reply....
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan
Please don't provide any code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic