function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PrasanthiPrasanthi 

sorting list items by last modified date

Hi,
I have a requirement where I need to display list of alerts sorted by last modified date. The problem here is there are 2 record types for Alerts and we are retreiving Alerts records for each record type in 2 different queries and ading them to the same list. Now we want to sort this final list by last modified date. I found that we can do this using a wrapper class that implemnts comparable but noticed that it returns the value not a list.

This is the code which is storing records into a list
public void alertUpdate()
    {
        alertsList=new List<Community_Content__c>();
        updatesList=new List<Community_Content__c>();
           
     // retreiving Alerts of Event record type   
        for(Community_Content__c comm: [SELECT Id,Alert_Text__c,Alert_Status__c,RecordTypeId,Update_Text__c, Update_Title__c ,Update_Status__c,LastModifiedDate
                                                  FROM Community_Content__c     
                                                  where RecordTypeId=:rtEvent.Id  
                                                  and (Alert_Status__c != 'Inactive' or Update_Status__c!='Inactive')
                                                  and (Alert_Text__c != null or Update_Text__c!=null)
                                                  and Event__c=:EventId order by LastModifiedDate desc])
                                        {
                                            if(comm.Alert_Status__c != 'Inactive' && comm.Alert_Status__c != null && comm.Alert_Text__c != null)
                                            alertsList.add(comm); // Adding alerts to a common list
                                            if(comm.Update_Status__c!= 'Inactive' && comm.Update_Status__c != null && comm.Update_Text__c!=null)
                                            updatesList.add(comm);
                                       }
    // retreiving alerts of Partner record type
    for(Community_Content__c comm1: [SELECT Id,Alert_Text__c,Alert_Status__c,RecordTypeId,Update_Text__c, 
                                    Update_Title__c ,Update_Status__c,Event_Partner__r.Accounts__c,LastModifiedDate
                                    FROM Community_Content__c     
                                    where RecordTypeId=:rtPartnerAlert.Id  
                                    and (Alert_Status__c != 'Inactive' or Update_Status__c!='Inactive')
                                    and (Alert_Text__c != null or Update_Text__c!=null)
                                    and Event__c=:EventId
                                    and Event_Partner__r.Accounts__c=:accountid order by LastModifiedDate desc]) 
                                    
                                    {
                                            if(comm1.Alert_Status__c != 'Inactive' && comm1.Alert_Status__c != null && comm1.Alert_Text__c != null)
                                            alertsList.add(comm1);  // Adding alerts to a common list
                                            if(comm1.Update_Status__c!='Inactive' && comm1.Update_Status__c!= null && comm1.Update_Text__c!=null)
                                            updatesList.add(comm1);
                                    }
    }

Now I want to sort alertsList by Last Modified Date.

Any help is deeply appreciated. Thank you.
PrasadVRPrasadVR
Hi Prasanthi ,

I am not sure about ur Business requirement  , But My question is why Don't use single query to pull all the records some thing  like this ?
the below query will pull all the records based LastModified Date .

     // retreiving Alerts of Event record type   
        for(Community_Content__c comm: [SELECT Id,Alert_Text__c,Alert_Status__c,RecordTypeId,Update_Text__c, Update_Title__c ,Update_Status__c,LastModifiedDate
                                                  FROM Community_Content__c     
                                                  where (RecordTypeId=:rtEvent.Id OR RecordTypeId=:rtPartnerAlert.Id) AND
                                                  ((Alert_Status__c != 'Inactive' OR Update_Status__c!='Inactive') AND
                                                   (Alert_Text__c != null or Update_Text__c!=null)) AND (Event__c=:EventId OR(Event__c=:EventId AND (Event_Partner__r.Accounts__c=:accountid))) order by LastModifiedDate desc]
                                                  
                                        {
                                            if(comm.Alert_Status__c != 'Inactive' && comm.Alert_Status__c != null && comm.Alert_Text__c != null)
                                            alertsList.add(comm); // Adding alerts to a common list
                                            if(comm.Update_Status__c!= 'Inactive' && comm.Update_Status__c != null && comm.Update_Text__c!=null)
                                            updatesList.add(comm);
                                        }
PrasanthiPrasanthi
Hi Prasad

The Business requirement is User should be able to see all the records related to Event Record Type and for Partner Record type user should view only the records that matches the criteria i.e., Event_Partner__r.Accounts__c=:accountid 

If we use a single query Event__c=:EventId will always return true, irrespective of the other condition in this way we are retreiving all the Alerts.

 for(Community_Content__c comm: [SELECT Id,Alert_Text__c,Alert_Status__c,RecordTypeId,Update_Text__c, Update_Title__c ,Update_Status__c,LastModifiedDate
                                                  FROM Community_Content__c     
                                                  where (RecordTypeId=:rtEvent.Id OR RecordTypeId=:rtPartnerAlert.Id) AND
                                                  ((Alert_Status__c != 'Inactive' OR Update_Status__c!='Inactive') AND
                                                   (Alert_Text__c != null or Update_Text__c!=null)) AND (Event__c=:EventId OR(Event__c=:EventId AND (Event_Partner__r.Accounts__c=:accountid))) order by LastModifiedDate desc]