public int compare(Object obj1, Object obj2) { ShoppingItem item1 = (ShoppingItem) obj1; ShoppingItem item2 = (ShoppingItem) obj2; Long categoryId1 = new Long(item1.getCategoryId()); Long categoryId2 = new Long(item2.getCategoryId()); int value = categoryId1.compareTo(categoryId2); if (value == 0) { if (item1.getMinQuantity() < item2.getMinQuantity()) { value = -1; } else if (item1.getMinQuantity() > item2.getMinQuantity()) { value = 1; } } if (value == 0) { value = item1.getName().toLowerCase().compareTo(item2.getName().toLowerCase()); } if (_ascending) { return value; } else { return -value; } }