+ Start a Discussion
Michael MMichael M 

Sorting List<Map<Date,DataWrapper>> in order of date (key) values

Hello, I have a List<Map<Date,DataWrapper>>. The DataWrapper class looks like this: 
Public Class DataWrapper{
String toDate;
String NPI;
String locationType;    
}

I need to be able to do the following:
1) sort the items in the list in order of Date (the key values of each map), then 
2) extract the String values from each datawrapper class
3) save ALL values (date, string, string, string) from ALL list-items to fields on my custom object. Can someone assist in how to accomplish all of these steps? Thank you so much i really appreciate any help with writing this code.
ShivankurShivankur (Salesforce Developers) 
Hi Michael,

I see this question has been addressed on below question link: https://developer.salesforce.com/forums/?id=9062I000000DIRJQA4,

According to which "Once your map is filled then after iterating over the map of keyset you can get the values." You can take a look with the provided code over it and then should be able to achieve the requirement posted above with slight modifications.

Please mark as Best Answer so that it can help others in the future.

Thanks.
Michael MMichael M
HI Shivankur,

Thank you for your reply. I am not sure I understand how to apply this to my scenario. In my case, I first need to sort the items of my list in order of date. After I do that, i need to get Item 1 in list and its corresponding data wrapper, than Item 2 in list and its corresponding data wrapper. Here is what I am attempting, but it is not working. The part in BOLD font is where I am getting stuck. Does this look right?

public class SeekPartAStays {

   public static void savePartAStaysDetails2(id EligibilityRecordid, List<Map<Date,SeekExtension.DataWrapper>> masterList){
       system.debug('MASTER LIST: ' + masterList);
    Eligibility__c eligRec = [select id from eligibility__c where id = :EligibilityRecordid];
      List<date> fromDates = new list<date>();
       Map<Date, SeekExtension.DataWrapper> dw1 = masterList[0];
       Map<Date, SeekExtension.DataWrapper> dw2;
       Map<Date, SeekExtension.DataWrapper> dw3;
       Map<Date, SeekExtension.DataWrapper> dw4;
       Map<Date, SeekExtension.DataWrapper> dw5;
       Map<Date, SeekExtension.DataWrapper> dw6;
       Map<Date, SeekExtension.DataWrapper> dw7;
       Map<Date, SeekExtension.DataWrapper> dw8;
       Map<Date, SeekExtension.DataWrapper> dw9;
       Map<Date, SeekExtension.DataWrapper> dw10;
       Map<Date, SeekExtension.DataWrapper> dw11;
       Map<Date, SeekExtension.DataWrapper> dw12;

         fromDates.addall(dw1.keyset());
       
       if (masterList.size() > 1){
           dw2 = masterList[1];
         fromDates.addall(dw2.keyset());
       }
       if (masterList.size() > 2){
           dw3 = masterList[2];
         fromDates.addall(dw3.keyset());
       }
       if (masterList.size() > 3){
           dw4 = masterList[3];
         fromDates.addall(dw4.keyset());
       }
       if (masterList.size() > 4){
           dw5 = masterList[4];
         fromDates.addall(dw5.keyset());
       }
       if (masterList.size() > 5){
           dw6 = masterList[5];
         fromDates.addall(dw6.keyset());
       }
       if (masterList.size() > 6){
           dw7 = masterList[6];
         fromDates.addall(dw7.keyset());
       }
       if (masterList.size() > 7){
           dw8 = masterList[7];
         fromDates.addall(dw8.keyset());
       }
       if (masterList.size() > 8){
           dw9 = masterList[8];
         fromDates.addall(dw9.keyset());
       }
       if (masterList.size() > 9){
           dw10 = masterList[9];
         fromDates.addall(dw10.keyset());
       }
       if (masterList.size() > 10){
           dw11 = masterList[10];
         fromDates.addall(dw11.keyset());
       }
       if (masterList.size() > 11){
           dw12 = masterList[11];
         fromDates.addall(dw12.keyset());
       }
       system.debug('MAP 1: ' + dw1);
       system.debug('MAP 2: ' + dw2);
       system.debug('MAP 3: ' + dw3);
       system.debug('MAP 4: ' + dw4);
       system.debug('MAP 5: ' + dw5);
       system.debug('MAP 6: ' + dw6);
       system.debug('MAP 7: ' + dw7);
       system.debug('MAP 8: ' + dw8);
       
        fromDates.sort();
                            system.debug('FROM DATES FIRST LIST: ' + fromDates) ;
      List<date> reversedFromDatesList = new List<date>();
        for(Integer i = fromDates.size() - 1; i >= 0; i--){
            reversedFromDatesList.add(fromDates[i]);
        }
       system.debug('REVERSED DATES LIST: ' + reversedFromDatesList) ;
       date d = date.today();
       reversedFromDatesList.add(d);
       system.debug('reversed list item 1: ' + reversedFromDatesList[0]);
       system.debug('reversed list item 2: ' + reversedFromDatesList[1]);
       system.debug('reversed list item 3: ' + reversedFromDatesList[2]);
       system.debug('reversed list item 4: ' + reversedFromDatesList[3]);
       system.debug('reversed list item 5: ' + reversedFromDatesList[4]);
       system.debug('reversed list item 6: ' + reversedFromDatesList[5]);
       system.debug('reversed list item 7: ' + reversedFromDatesList[6]);
       system.debug('reversed list item 8: ' + reversedFromDatesList[7]);
       
       SeekExtension.DataWrapper wrapper1;
       system.debug('WRAPPER1: ' + wrapper1) ;
       SeekExtension.DataWrapper wrapper2;
       SeekExtension.DataWrapper wrapper3;
       SeekExtension.DataWrapper wrapper4;
       SeekExtension.DataWrapper wrapper5;
       SeekExtension.DataWrapper wrapper6;
       SeekExtension.DataWrapper wrapper7;
       SeekExtension.DataWrapper wrapper8;
       SeekExtension.DataWrapper wrapper9;
       SeekExtension.DataWrapper wrapper10;
       SeekExtension.DataWrapper wrapper11;
       SeekExtension.DataWrapper wrapper12;   
       for (date ddd : reversedFromDatesList){
 for (Map<Date,SeekExtension.DataWrapper> dddd : masterList){
     wrapper1 = dddd.get(ddd);
     wrapper2 = dddd.get(ddd);
     wrapper3 = dddd.get(ddd);
     wrapper4 = dddd.get(ddd);
     wrapper5 = dddd.get(ddd);
     wrapper6 = dddd.get(ddd);
     wrapper7 = dddd.get(ddd);
     wrapper8 = dddd.get(ddd);
     wrapper9 = dddd.get(ddd);
     wrapper10 = dddd.get(ddd);
     wrapper11 = dddd.get(ddd);
     wrapper12 = dddd.get(ddd);
     system.debug('wrapper1: ' +wrapper1 );
     system.debug('wrapper2: ' +wrapper2);
     system.debug('wrapper3: ' +wrapper3 );
     system.debug('wrapper4: ' +wrapper4 );
     system.debug('wrapper5: ' +wrapper5 );
     system.debug('wrapper6: ' +wrapper6 );
     system.debug('wrapper7: ' +wrapper7 );
     system.debug('wrapper8: ' +wrapper8 );
 }}
       string fromDate1 = string.valueof(reversedFromDatesList[0]);                                       
       string fromDate2;                                       
       string fromDate3;                                       
       string fromDate4;                                       
       string fromDate5;                                       
       string fromDate6;                                       
       string fromDate7;                                       
       string fromDate8;                                     
       string fromDate9;                                      
       string fromDate10;                                      
       string fromDate11;                                      
       string fromDate12;
       string toDate1= wrapper1.toDate;
       string toDate2;
       string toDate3;
       string toDate4;
       string toDate5;
       string toDate6;
       string toDate7;
       string toDate8;
       string toDate9;
       string toDate10;
       string toDate11;
       string toDate12;
       string npi1= wrapper1.NPI;
       string npi2;
       string npi3;
       string npi4;
       string npi5;
       string npi6;
       string npi7;
       string npi8;
       string npi9;
       string npi10;
       string npi11;
       string npi12;
       string locationType1= wrapper1.locationType;
       string locationType2;
       string locationType3;
       string locationType4;
       string locationType5;
       string locationType6;
       string locationType7;
       string locationType8;
       string locationType9;
       string locationType10;
       string locationType11;
       string locationType12;
       system.debug('AND AGAIN. REVERSED DATES LIST: ' + reversedFromDatesList) ;
       eligRec.seek_Hospital_from_date_1__c = fromDate1;
       eligRec.seek_Hospital_to_date_1__c = toDate1;
       eligRec.seek_Hospital_NPI_1__c = npi1;
       eligRec.CS_Description1__c = locationType1;
       system.debug('wrapper1: '+ wrapper1);
       system.debug('DW2: '+ dw2);
       
    for (Map<Date,SeekExtension.DataWrapper> dd : masterList){
      
        if (dd.get( reversedFromDatesList[0]) != null){
           wrapper2 = dw2.get(reversedFromDatesList[0]);
           system.debug('TRIAL wrapper2: ' + wrapper2);
           fromDate2 = string.valueof(reversedFromDatesList[1]); 
           if (wrapper2 != null){
           toDate2= wrapper2.toDate;
           npi2= wrapper2.NPI;
           locationType2= wrapper2.locationType;
           eligRec.seek_Hospital_from_date_2__c = fromDate2;
           eligRec.seek_Hospital_to_date_2__c = toDate2;
           eligRec.seek_Hospital_NPI_2__c = npi2;
           eligRec.CS_Description2__c = locationType2;
           }
       }
       if (reversedFromDatesList.size() >= 2){
           wrapper3 = dw3.get(reversedFromDatesList[3]);
            system.debug('wrapper3: ' + wrapper3);
           fromDate3 = string.valueof(reversedFromDatesList[2]); 
           toDate3= wrapper3.toDate;
           npi3= wrapper3.NPI;
           locationType3= wrapper3.locationType;
           eligRec.seek_Hospital_from_date_3__c = fromDate3;
           eligRec.seek_Hospital_to_date_3__c = toDate3;
           eligRec.seek_Hospital_NPI_3__c = npi3;
           eligRec.CS_Description3__c = locationType3;
       }
       if (reversedFromDatesList.size() > 3){
           wrapper4 = dw4.get(reversedFromDatesList[3]);
            system.debug('wrapper4: ' + wrapper4);
           fromDate4 = string.valueof(reversedFromDatesList[3]); 
           toDate4= wrapper4.toDate;
           npi4= wrapper4.NPI;
           locationType4= wrapper4.locationType;
           eligRec.seek_Hospital_from_date_4__c = fromDate4;
           eligRec.seek_Hospital_to_date_4__c = toDate4;
           eligRec.seek_Hospital_NPI_4__c = npi4;
           eligRec.CS_Description4__c = locationType4;
       }
       if (reversedFromDatesList.size() > 4){
           wrapper5 = dw5.get(reversedFromDatesList[4]);
           fromDate5 = string.valueof(reversedFromDatesList[4]); 
           toDate5= wrapper5.toDate;
           npi5= wrapper5.NPI;
           locationType5= wrapper5.locationType;
           eligRec.seek_Hospital_from_date_5__c = fromDate5;
           eligRec.seek_Hospital_to_date_5__c = toDate5;
           eligRec.seek_Hospital_NPI_5__c = npi5;
           eligRec.CS_Description5__c = locationType5;
       }
       if (reversedFromDatesList.size() > 5){
           wrapper6 = dw6.get(reversedFromDatesList[5]);
           fromDate6 = string.valueof(reversedFromDatesList[5]); 
           toDate6= wrapper6.toDate;
           npi6= wrapper6.NPI;
           locationType6= wrapper6.locationType;
           eligRec.seek_Hospital_from_date_6__c = fromDate6;
           eligRec.seek_Hospital_to_date_6__c = toDate6;
           eligRec.seek_Hospital_NPI_6__c = npi6;
           eligRec.CS_Description6__c = locationType6;
       }
       if (reversedFromDatesList.size() > 6){
           wrapper7 = dw7.get(reversedFromDatesList[6]);
           fromDate7 = string.valueof(reversedFromDatesList[7]); 
           toDate7= wrapper7.toDate;
           npi7= wrapper7.NPI;
           locationType7= wrapper7.locationType;
           eligRec.seek_SNF_from_date_1__c = fromDate7;
           eligRec.seek_SNF_to_date_1__c = toDate7;
           eligRec.seek_SNF_NPI_1__c = npi7;
           eligRec.CS_Description7__c = locationType7;
       }
       if (reversedFromDatesList.size() > 7){
           wrapper8 = dw8.get(reversedFromDatesList[7]);
           fromDate8 = string.valueof(reversedFromDatesList[7]); 
           toDate8= wrapper8.toDate;
           npi8= wrapper8.NPI;
           locationType8= wrapper8.locationType;
           eligRec.seek_SNF_from_date_2__c = fromDate8;
           eligRec.seek_SNF_to_date_2__c = toDate8;
           eligRec.seek_SNF_NPI_2__c = npi8;
           eligRec.CS_Description8__c = locationType8;
       }
       if (reversedFromDatesList.size() > 8){
           wrapper9 = dw9.get(reversedFromDatesList[8]);
           fromDate9 = string.valueof(reversedFromDatesList[8]); 
           toDate9= wrapper9.toDate;
           npi9= wrapper9.NPI;
           locationType9= wrapper9.locationType;
           eligRec.seek_SNF_from_date_3__c = fromDate9;
           eligRec.seek_SNF_to_date_3__c = toDate9;
           eligRec.seek_SNF_NPI_3__c = npi9;
           eligRec.CS_Description9__c = locationType9;
       }
       if (reversedFromDatesList.size() > 9){
           wrapper10 = dw10.get(reversedFromDatesList[9]);
           fromDate10 = string.valueof(reversedFromDatesList[9]); 
           toDate10= wrapper10.toDate;
           npi10= wrapper10.NPI;
           locationType10= wrapper10.locationType;
           eligRec.seek_SNF_from_date_4__c = fromDate10;
           eligRec.seek_SNF_to_date_4__c = toDate10;
           eligRec.seek_SNF_NPI_4__c = npi10;
           eligRec.CS_Description10__c = locationType10;
       }
       if (reversedFromDatesList.size() > 10){
           wrapper11 = dw11.get(reversedFromDatesList[10]);
           fromDate11 = string.valueof(reversedFromDatesList[10]); 
           toDate11= wrapper11.toDate;
           npi11= wrapper11.NPI;
           locationType11= wrapper11.locationType;
           eligRec.seek_SNF_from_date_5__c = fromDate11;
           eligRec.seek_SNF_to_date_5__c = toDate11;
           eligRec.seek_SNF_NPI_5__c = npi11;
           eligRec.CS_Description11__c = locationType11;
       }
       if (reversedFromDatesList.size() > 11){
           wrapper12 = dw12.get(reversedFromDatesList[11]);
           fromDate12 = string.valueof(reversedFromDatesList[11]); 
           toDate12= wrapper12.toDate;
           npi12= wrapper12.NPI;
           locationType12= wrapper12.locationType;
           eligRec.seek_SNF_from_date_6__c = fromDate12;
           eligRec.seek_SNF_to_date_6__c = toDate12;
           eligRec.seek_SNF_NPI_6__c = npi12;
           eligRec.CS_Description12__c = locationType12;
       }
    }

    update EligRec;   
       
       
       }}