I have a List of Objects like List<Object> p.I want to sort this list alphabetically using Object name field. Object contains 10 field and name field is one of them.
if (list.size() > 0) {
Collections.sort(list, new Comparator<Campaign>() {
@Override
public int compare(final Object object1, final Object object2) {
return String.compare(object1.getName(), object2.getName());
}
} );
}
But there is nothing like String.compare..?
Objects don't have names. Do you mean using.toString()?object1andobject2need to be of typeCampaign, and the compare function isobject1.getName().compareTo(object2.getName()).List<Object>andComparator<Campaign>. You can't do that. Either you haveList<Object>andComparator<Object>orList<Campaign>andComparator<Campaign>