Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting

 
divya kundapur
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());
 
Vinoth Kumar Kannan
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic