The case: I have an ArrayList of objects, which has the following print method:
public void printAllItems()
{
if(itemsList.isEmpty())
{
System.out.println("The list is empty");
}
else
{
System.out.println("Number of items: " + itemCount);
for(ShopItem shopItem : itemsList)
{
shopItem.printDetails();
System.out.println(); // empty line between posts
}
}
}
Now I want to sort those objects Alphabetically by one of their fields(itemName
and of course its suitable getItemName()
method). I went through the Java doc and found that I should use Comparator. The thing is that I didn't actually understood how to implement the Comparator code into the for-loop. Any suggestions ?
P.S. I am sorry, if the question looks awfully easy, but I am still a beginner and I get confused easily :(