-3

I am running the below code

List<AggregateResult> lstWO = [select Id, AccountId, MAX(Completed_Date__c) CompletedDate from WorkOrder WHERE Order__c IN (Select Id from Order WHERE AccountId = 'XXXXXXXXXXXXXXX' AND  Status = 'Activated') AND Status = 'Completed' group by AccountId, Id];

// The above query will return a list of CompletedDate. The aim here is to compare the completed dates and output the most recent dates among those dates

List<compareDates> wo = new List<compareDates>();
for(AggregateResult w: lstWO) {
    wo.add(new compareDates((String)w.get('Id'),(Date)w.get('CompletedDate')));
    
}
system.debug('wo' + wo.size());




public class compareDates implements Comparable {
        public Id woId;
        public date woDate;
        public compareDates(String i, date n) {
            woId = i;
            woDate = n;
        }
        
        public Integer compareTo(Object compareTo) {
            compareDates comparable = (compareDates)compareTo;
            if (woDate > comparable.woDate) return 1;
            return 0;
        }
    }

I have a list of dates . From this list goal is to pick up most recent dates, by comparing among each other. So the output will be "2019-05-29","2019-05-29", "2019-03-27",

"2019-05-13"
"2019-05-29"
"2019-05-08"
"2019-05-29"
"2019-03-27"

The code is not working properly . Can anyone let me know what is the mistake i am doing

2
  • 1
    specifically - what is not working? Use edit
    – cropredy
    Mar 2, 2022 at 19:02
  • hi cropredy.. i have updated the question Mar 2, 2022 at 19:07

1 Answer 1

4

The compareTo() method of a class that implements the Comparable interface is not run when you add things to a list, it's run when you call .sort() on the list.

You also need to have compareTo() return 3 values:

  • 1, when you want the current item to have a higher rank than the item passed in for comparison)
  • 0, when the two items are to be treated as identical
  • -1, when you want to item passed in to have a higher rank than the current item

List's .sort() always sorts in ascending order, but we can get things sorted in descending order by swapping the conditions that return 1 and -1. That is to say that if you want the most recent date to be first in the sorted list, you want to have woDate > comparable.woDate return -1 instead of 1 (and woDate < comparable.woDate return 1).

Using comparable as a variable name isn't likely going to be the end of the world, but I'd consider it bad practice.

That said, you don't need comparable here

Although you're currently ignoring all Ids in your comparison, I assume that your task here is to find the most recently completed Work Order per account.

With appropriate grouping and ordering, you can keep nearly all of the work in SOQL.

  • GROUP BY AccountId, CompletedDate, Id
  • ORDER BY AccountId, Completed_Date__c DESC
  • Iterate over the results, and only add to a map (keyed on the Account Id) if the current AccountId doesn't exist in the map

The GROUP BY and ORDER BY gets you the data in the appropriate order.
By only putting into a map on the first encounter of your AccountId, you're ensuring that you only get the most recent date for that AccountId

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .