Hi I'm trying to sort an object based on a value of that object. Originally I used TreeMap which did the sorting but there were some non-unique keys which were removed so that didn't work. I googled around and came across Guava MultiMap. But how do I actually use this to sort the values?
Basically I'm trying to sort budget information based on the diffYTD_percentage value. Thanks!
Multimap<Double, BudgetInformation> multiMap = ArrayListMultimap.create();
for (Budget budget : budgets) {
BudgetInformation budgetInfo = getBudget(budget.getId());
double actualToDate = budgetInfo.getActualToDate();
double budgetToDate = budgetInfo.getTotalBudgetToDate();
double diffYTD_value = budgetToDate - actualToDate;
double diffYTD_percentage_value = 0.0;
if (budgetToDate != 0.0) {
double fraction = actualToDate / budgetToDate;
fraction = fraction - 1;
diffYTD_percentage_value = fraction * 100;
}
multiMap.put(diffYTD_percentage_value, budgetInfo);
}
Iterator<BudgetInformation> budgetIterator = multiMap.values().iterator();