File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collections.sort() on Array List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collections.sort() on Array List" Watch "Collections.sort() on Array List" New topic

Collections.sort() on Array List

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434

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?

Marriage Made in Heaven
Tom Reilly

Joined: Jun 01, 2010
Posts: 618
Take a look at java.util.Comparator
Alpesh Padra
Ranch Hand

Joined: Jan 10, 2010
Posts: 41
You can implement sorting by 2 way

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

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Is this you want? Check and reply....

|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Abimaran Kugathasan
Please don't provide any code.

I agree. Here's the link:
subject: Collections.sort() on Array List
It's not a secret anymore!