• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting  RSS feed

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
 
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 21136
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!