Java Comparator for Objects mit mehreren Feldern [geschlossen]
Ich habe ein ObjektCollection
mit 5 Feldern:
id;
entityType;
entityId;
brandId;
productId;
Eine sortierenArrayList
vonCollection
Ich habe folgendes geschriebenComparaor
.
Comparator<Collection> collectionComparator = new Comparator<Collection>() {
@Override
public int compare(Collection collection1, Collection collection2) {
if(collection1.getId().equals(collection2.getId())) {
if(collection1.getEntityType().equals(collection2.getEntityType())) {
if(collection1.getEntityId().equals(collection2.getEntityId())) {
if(collection1.getBrandId().equals(collection2.getBrandId())) {
return collection1.getProductId().compareTo(collection2.getProductId());
} else {
return collection1.getBrandId().compareTo(collection2.getBrandId());
}
} else {
return collection1.getEntityId().compareTo(collection2.getEntityId());
}
} else {
return collection1.getEntityType().compareTo(collection2.getEntityType());
}
}
return collection1.getId().compareTo(collection2.getId());
}
};
Ist das der richtige Weg umzusetzen?Comparator
Auf dem Objekt, das mehrere Felder zum Vergleichen hat?