Comparador de Java para objetos con múltiples campos [cerrado]
Tengo un objetoCollection
con 5 campos:
id;
entityType;
entityId;
brandId;
productId;
Para ordenar unArrayList
deCollection
He escrito lo siguienteComparaor
.
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());
}
};
¿Es esta la forma correcta de implementarComparator
en el objeto que tiene múltiples campos para comparar?