I have following controller code which is used to plot a chart. The code returns all the values correctly, but is not sorting the data despite including implements Comparable...Please help.

Controller:

public List<OpportunityData> getOpportunityData() {
Map<String, OpportunityData> oppToData = new Map<String, OpportunityData>();
Integer regnInt = (regn!=null) ? Integer.valueOf(regn) : -1;  
AggregateResult[] result = [SELECT Month__c, Year__c, Sum(Total_Rental_Revenue_Actual__c) Reve, Sum(Rental_Revenue_Goal__c) Gol FROM User_Stats__C 
where ((Month__c = : string.valueof(today.month()) AND Year__c = : string.valueof(today.year()))
OR (Month__c = : string.valueof(monthPlus1.month()) AND Year__c = : string.valueof(monthPlus1.year()))
OR (Month__c = : string.valueof(monthPlus2.month()) AND Year__c = : string.valueof(monthPlus2.year()))) 
and Region__c =: regnInt and District__c =: dist and Sales_Rep__c != null and Rental_Revenue_Goal__c != null and Rental_Revenue_Goal__c != 0 GROUP BY Year__c, Month__c ORDER BY Year__c asc, Month__c asc];
for(AggregateResult a : result) {
    OpportunityData opp = new OpportunityData((String)a.get('Month__c'),
                                          (String)a.get('Year__c'),
                                          (Double)a.get('Reve'),
                                          (Double)a.get('Gol'),
                       0);
    oppToData.put((String)a.get('Month__c'), opp);
}

result = [Select Name, Year__c, Month__c, Sum(Rental_Revenue_Plan__c) DisRev From District_Stat__c 
where ((Month__c = : string.valueof(today.month()) AND Year__c = : string.valueof(today.year()))
OR (Month__c = : string.valueof(monthPlus1.month()) AND Year__c = : string.valueof(monthPlus1.year()))
OR (Month__c = : string.valueof(monthPlus2.month()) AND Year__c = : string.valueof(monthPlus2.year()))) 
    and Name =: dist and Name != null
    group by Name, Year__c, Month__c ORDER BY Year__c asc, Month__c asc ];
for(AggregateResult a:result) {
    if (!oppToData.containsKey((String) a.get('Month__c'))) {
        OpportunityData opp = new OpportunityData((String)a.get('Month__c'),
            (String)a.get('Year__c'),
            0,
    0,
            (Decimal)a.get('DisRev'));
        oppToData.put((String) a.get('Month__c'), opp);
    } else {
        oppToData.get((String) a.get('Month__c')).DistRev = ((Decimal)a.get('DisRev'))/1000;
    }
}
return oppToData.values();
}

public class OpportunityData implements Comparable
{
public Integer compareTo(Object o) {
return revenueDate-((OpportunityData)o).revenueDate;
}
public Integer month { get; set; }
public Integer year { get; set; }
public String monthString { get; set; }    
public Integer revenueDate { get; set; }     
public Decimal Revenue { get; set; }
public Decimal Goal { get; set; }
public Decimal Attnmnt { get; set; }    
public Decimal DistRev { get; set; } 

public OpportunityData(String Mon, String Yr, Decimal Reve, Decimal Gol, Decimal Disrev) {
this.month = integer.valueof(Mon);
this.year = integer.valueof(Yr);
    this.revenueDate = Year * 100 + month;
monthString = DateTime.newInstanceGMT(System.Date.newInstance(2000, month, 1), System.Time.newInstance(0, 0, 0, 0)).formatGMT('MMM');
Revenue = Reve/1000;
Goal = Gol/1000;
Attnmnt = Revenue / Goal * 100;
DistRev = Disrev / 1000;
    }
}
up vote 5 down vote accepted

You need to call sort() on your list to actually sort the data after implementing Comparable.

Try replacing:

return oppToData.values();

With:

List<OpportunityData> sortedResult = oppToData.values();
sortedResult.sort();
return sortedResult;

The comparable interface requires you to implement the compareTo() method, which you are doing. The issue is you are not implementing that method correctly. That method returns an integer based on the comparison. You are subtracting one date from another and trying to return that.

Try something like this in your compareTo()

public Integer compareTo(Object compareTo) {
      OpportunityData oppData = (OpportunityData)compareTo;
      if (revenueDate == oppData.revenueDate){
          return 0;
      } 
      if (revenueDate > oppData.revenueDate){
          return -1;
      }
      return 1;  
}
  • If you look at his code you'll notice that revenueDate is an Integer. So whilst his does not return the ideal values of 1, 0 or -1 it will return an Integer that works for the Comparable interface. – Alex Tennant Feb 12 '14 at 12:08

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.