I\'m new to java, and I\'m not really getting how to use the comparator interface.
I have an ArrayList of Items in an Inventory class
Use of @Override annotation is a standard practice in editors like eclipse, netbeans to notify developer that he is overriding/implementing parent class/interface method. It is optional.
Don't implement this interface in your Item class. Create a new class and implement the Comparator interface.
public class ItemCompare implements Comparator- {
@Override
public int compare(Item a, Item b) {
if (a.getID().compareToIgnoreCase(b.getID())>0)
return 1;
else if (a.getID().compareToIgnoreCase(b.getID())<0)
return -1;
return 0;
}
}
And then, in your main class, do this:
ArrayList al = new ArrayList-
Collections.sort(al, new ItemCompare())