I have the following list of objects of type Supplier and I want to sort them using the reverseOrder() method (so they will be in descending order). However, after reading whole day on the internet, I still can't get this working. I'm pretty sure that this is something really small that I am missing here. The natural order in ascending order works just fine.
Here is my Supplier class:
public class Supplier {
private String supplierName = "";
private String representative = "";
private String representativesPhoneNumber = "";
private Map<Drug, Integer> listOfDrugs = new HashMap<Drug, Integer>();
Supplier(String n, String rep, String repPhoneNum, String drugName, double drugPrice, int stock) {
this.supplierName = n;
this.representative = rep;
this.representativesPhoneNumber = repPhoneNum;
listOfDrugs.put(new Drug(drugName, drugPrice), stock);
}
public Map<Drug, Integer> getListOfDrugs() {
return this.listOfDrugs;
}
public static Integer getKeyExtractor(Supplier supplier, Drug drug) {
return Optional.ofNullable(Optional.ofNullable(supplier.getListOfDrugs())
.orElseThrow(() -> new IllegalArgumentException("drugs is null")).get(drug))
.orElseThrow(() -> new IllegalArgumentException("the drug couldn't be found"));
}
}
It has a Map if objects <Drug, Integer>.
Here is my Drug class:
public class Drug {
private String name = "";
private double price = 0.0;
Drug(String n, double p) {
this.name = n;
this.price = p;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
long temp;
temp = Double.doubleToLongBits(price);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Drug other = (Drug) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
return false;
return true;
}
}
Most of the code is trimmed, for the sake of spam. :)
And my Orders class, where I actually do the sorting:
public class Orders {
private Map <Drug, Integer> orderedDrugs = new HashMap <Drug, Integer>();
private Vector<Supplier> suppliers = new Vector <Supplier>();
public void sort(Drug drug, List<Supplier> sortedSuppliers) {
Collections.sort(suppliers, Comparator.comparing(s -> Supplier.getKeyExtractor(s, drug), Comparator.reverseOrder()));
}
public List<Supplier> getSortedSuppliersByQuantity(Drug drug) {
List <Supplier> sortedSuppliers = new ArrayList <Supplier>();
for(Supplier s : suppliers) {
for(Entry<Drug, Integer> entry : s.getListOfDrugs().entrySet()) {
if(entry.getKey().getDrugsName().equals(drug.getDrugsName()));
sortedSuppliers.add(s);
}
}
sort(drug, sortedSuppliers);
return sortedSuppliers;
}
}
The code is trimmed again, only displaying the needed methods for the actual problem.
So I've tried so far with:
Collections.sort(suppliers, Comparator.comparing(s -> Supplier.getKeyExtractor(s, drug), Comparator.reverseOrder()));Collections.sort(suppliers, Collections.reverseOrder(Comparator.comparing(s -> Supplier.getKeyExtractor(s, drug))));
But both don't work. Do I need to implement compareTo() somewhere or am I missing some method? Since ascending order is working, but not descending.
Going with Collections.sort(suppliers, Comparator.comparing(s -> Supplier.getKeyExtractor(s, drug))); sorts them in ascending order and works.
Thank you for the help in advance and I'm sorry for the long post!
UPDATE:
I have also tried to implement compareTo in the Supplier class, but I get a NPE. :/
public int compareTo(Supplier a) {
for(Entry<Drug, Integer> entry : listOfDrugs.entrySet()) {
int result = listOfDrugs.get(entry.getKey()).compareTo(a.listOfDrugs.get(entry.getKey()));
if(result != 0)
return result;
}
return 0;
}
Collections.sort(suppliers, Comparator.comparing(s -> Supplier.getKeyExtractor(s, drug)))in mysortmethod it works and it sorts the list in ascending order, but when I use the examples withreverseOrders()it doesn't sort the list at all.compareTobut I get a NPE.