I needed to sort a list of Records by a Date field, so I created a custom sort wrapper class. Now that it is sorted, I want a single one of these records from the list, however I am running into a problem: Illegal assignment from CWDHWrapperSortByDate to CWDH__c

The wrapper class takes in CWDH__c records, and the assignment I am trying to do is also to a CWDH__c record. I'm not sure how to properly do this assignment.

Here is my code with the error coming on the last line:

CWDH__c wdhToUse = new CWDH__c();
List<CWDH__c> calculatedWDHList = new List<CWDH__c>();        
List<CWDHWrapperSortByDate> sortedLst = new List<CWDHWrapperSortByDate>();
for (CWDH__c cwdh : calculatedWDHList) {
   if (cwdh.Timely_Status__c == 'Timely')   {
          sortedTimelyLst.add(new CWDHWrapperSortByDate(cwdh)); 
   if (sortedTimelyLst.size() > 0) {
       wdhToUse = sortedTimelyLst.get(sortedTimelyLst.size());

Here is the sorting class:

public class CWDHWrapperSortByDate implements Comparable {

    public CWDH__c cwdh {get;set;}
    //Constructor for setting the public member variable for CWDH:
    public CWDHWrapperSortByDate( CWDH__c sItem ){
        this.cwdh = sItem;
    public Integer compareTo(Object compareTo){
        CWDHWrapperSortByDate otherItem = (CWDHWrapperSortByDate)compareTo;
        Integer retVal = 0;
        //If the dates match each other:
        if ( this.cwdh.Work_Date__c == otherItem.cwdh.Work_Date__c ){
            retVal = 0;
        //If the current date is earlier than the compared date
        if ( this.cwdh.Work_Date__c < otherItem.cwdh.Work_Date__c ){
            retVal = -1;
        //If the current date is more recent than the compared date
        if ( this.cwdh.Work_Date__c > otherItem.cwdh.Work_Date__c ){
            retVal = 1;
        return retVal;

What is the proper way to assign this wdhToUse variable?

1 Answer 1


Your .get() from the list returns the wrapper itself. You want the record that is inside the wrapper instance.

For clarity, I broke it out into two lines. You can recombine the syntax into a single line if that's how you'd rather implement it.

if (sortedTimelyLst.size() > 0) {
    CWDHWrapperSortByDate wrapperThing = sortedTimelyLst.get(sortedTimelyLst.size());
    wdhToUse = wrapperThing.cwdh;

You must log in to answer this question.

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