This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Java in General and the fly likes Sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting " Watch "Sorting " New topic
Author

Sorting

divya kundapur
Ranch Hand

Joined: Aug 21, 2007
Posts: 110
I have an array 'items' which have objects that need to be compared and sorted, but the array will have objects that are not mutually comparable, how can i make sure the objects which are not mutually comparable be at the end ?

I know the below one does have work when objects are not mutually comparable , should i implement sorting in this case.

Arrays.sort(items, new ItemNameComparator());


SCJP - 5.0
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

You will have to write a class that implements the Comparator interface and pass that object to Arrays.sort(). So, you must override compare() and equals() methods of Comparator.


OCPJP 6
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20078
    
  30

Only compare(). The equals method is used to check if another object is equal to the Comparator itself, and is hardly ever needed for Comparator implementations.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Sorting
 
It's not a secret anymore!