I am trying to improve given algorithm of selecting a Storage box based on Summary info on the box. In order to retrieve the identifer (of summary object) which has the with the highest numItems attribute (of summary object), I would have to sort the Summary[], an array of objects but I just can't figure how to sort by an attribute.
I found numerous examples of creating ArrayList<Int> a = new ArrayList<Int>(); and then Using Collections to get maximum value but here, I am interested in other attribute and I just can't visualize how I would do that. Can you help?
public String selectNextDelivery(StorageBox.Summary[] summaries) throws NoBoxReadyException {
if (summaries.length != 0) {
for(StorageBox.Summary summary : summaries){
if (summary.numItems > 0) {
return summary.identifier;
}
}
}
// Otherwise no box is ready
throw new NoBoxReadyException();
}
Comparableinterface by implementing it in yourSummaryclass