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
Dinesh RDinesh R 

Wrapper List sorting

Hi All
Anyone give any idea regarding on sorting the wrapper list.
I have combined task and event into a single list using the wrapper class. Using the list I am showing the information to the VF page. But I want to show the result descending order so that I want to sort the list.

Please, anyone, give an idea to resolve the issue.
Best Answer chosen by Dinesh R
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Dinesh , 

Remove the inner class from the main controller class, 

and create a new class as a Wrapper class and refer it in the main controller class, 
 
global class eEventTTask implements Comparable {
        public String subject { get; set; }
        public Id whatId { get; set; }
        public Id ownerId { get; set; }
        public string whoid1{get;set;}
        public string whatIdName{get;set;}
        public string Description{get;set;}
        public string OwnerName{get;set;}
        public Date activityDate { get; set;}
        public DateTime lastModified { get; set;}
        public DateTime createdDate {get;set;}
        public eEventTTask(String sub, Id wht, Id own, Date act, Datetime lmd, String whoname, String whatName,string Description,string OwnerName,dateTime cd) {
            this.subject = sub;
            this.whatId = wht;
            this.whoId1  =whoname;
            this.whatIdName = whatName;
            this.description =Description;
            this.OwnerName = OwnerName;
            this.createdDate = cd;
            this.ownerId = own;
            this.activityDate = act;
            this.lastModified = lmd;
        }
		
		// Implement the compareTo() method
		global Integer compareTo(Object compareTo) {
			eEventTTask compareToEventTask = (eEventTTask)compareTo;
			if (activityDate == compareToEventTask.activityDate) {
				return 0;
			}			
			if (activityDate > compareToEventTask.activityDate) {
				return 1;
			}			
			return -1;        
		}
    }

After that uncomment this line,
// taskEventList.sort();

Please let me know if any problem arises,

Thanks, 
Prosenjit.


 

All Answers

Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Dinesh, 

The Wrapper class need to implement Comparable interface and need to define compareTo() method.

Suppose your wrapper class name is Employee, this code snippet can help you,
 
global class Employee implements Comparable {

    public Long id;
    public String name;
    public String phone;
    
    // Constructor
    public Employee(Long i, String n, String p) {
        id = i;
        name = n;
        phone = p;
    }
    
    // Implement the compareTo() method
    global Integer compareTo(Object compareTo) {
        Employee compareToEmp = (Employee)compareTo;
        if (id == compareToEmp.id) 
            return 0;
        if (id > compareToEmp.id) 
             return 1;
        return -1;        
    }
}

Now yout can sort like this,
 
List<Employee> empList = new List<Employee>();
for(Integer i==0; i<10; i++) {
	Employee emp = Employee();
	emp.name = 'TEST Name';
	emp.phone = '345345';
	emplList.add(emp);
}
empList.sort();

 Thanks,
Prosenjit
Dinesh RDinesh R
Hi Prosenjit
Yeah. that good.the same thing I have tried but, it's not working for me.
 
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Dinesh , 

May I see Your Code ? and tell me by which reference field you want to show the result in descending order ?

Thanks
Prosenjit
Dinesh RDinesh R
Hi Prosenjit 

Can you give your email Id I will send the code?
 
Dinesh RDinesh R
Else Shall I post the code here? Please let me know
Prosenjit Sarkar 7Prosenjit Sarkar 7
You can post here 
Dinesh RDinesh R
This is My code
Apex class

public class ShowAllSObjectActivity{
    Id accountId = ApexPages.currentPage().getParameters().get('Id');
    public list<Task> tasklist{get;set;}
    PUBLIC LIST<Task>eEventTTask {get;set;}
    public List<Event>eventList{get;set;}
    public Set<id> LeadId;
    public Set<id> contactId;
    public Set<id> opportunityId;
    public string accountIdPrefix;
    public string LeadIdPrefix ;
    public string contactidPrifix ;
    public string EventIdPrefix ;
    public string opportunityIdPrefix ;
    public List<eEventTTask> taskEventList{get;set;}
    public ShowAllSObjectActivity(){
        taskEventList = new List<eEventTTask>();
        tasklist         = new list<Task>();
        eventList        = new list<Event>();
        LeadId           = new set<Id>();
        contactId        = new Set<Id>();
        opportunityId    = new set<id>();
        LeadIdPrefix     = Schema.SObjectType.Lead.getKeyPrefix();
        accountIdPrefix  = Schema.SObjectType.Account.getKeyPrefix();
        contactidPrifix  = Schema.SObjectType.Contact.getKeyPrefix(); 
        EventIdPrefix    = Schema.SObjectType.Event.getKeyPrefix(); 
        opportunityIdPrefix = Schema.SObjectType.opportunity.getKeyPrefix();
        if(accountId !=null){
            for(Lead insLead :[SELECT id,Name FROM Lead WHERE Account__c =: accountId]){
                LeadId.add(insLead.Id);
            }
            for(Contact coninst :[SELECT id,Name FROM contact WHERE accountid =: accountId]){
                contactId.add(coninst.Id);
            }
            For(opportunity oppInst :[SELECT id,Name FROM opportunity WHERE accountid =:accountId]){
                opportunityId.add(oppInst.Id);
            }
            for(Event newEvent :[SELECT id,Createddate,ActivityDate,WhoId,Subject,Who.Name,What.Name,Description,LastModifiedDate,Owner.Name,Type FROM Event WHERE whatId =:accountId OR WhoId =: ContactId OR WhoId =: LeadID Order by ActivityDate DESC]){
                taskEventList.add(new eEventTTask(newEvent.Subject,newEvent.Id,newEvent.OwnerId,newEvent.ActivityDate, newEvent.LastModifiedDate,newEvent.Who.name,newEvent.What.name,newEvent.Description,newEvent.owner.Name,newEvent.CreatedDate));
                
            }
            for(Task  itTask :[select id,Status,Createddate,ActivityDate,WhoId,Subject,Who.Name,What.Name,Description,LastModifiedDate,Owner.Name FROM Task WHERE WhatID=:accountId OR WhoId =:contactId OR WhoId =:LeadId OR WhatID =: opportunityId Order by ActivityDate DESC]){
                system.debug('###itTask'+itTask);
                if((itTask.WhatId !=null)){
                    if(String.valueOf(itTask.whatId).startsWith(accountIdPrefix) || String.valueOf(itTask.WhatID).startsWith(opportunityIdPrefix)){
                        taskEventList.add(new eEventTTask(itTask.Subject,itTask.Id, itTask.OwnerId, itTask.ActivityDate, itTask.LastModifiedDate,itTask.Who.name,itTask.what.Name,itTask.Description,itTask.owner.Name,itTask.CreatedDate));
                        system.debug('@@@taskEventList'+taskEventList);
                        system.debug('****taskEventList.size()'+taskEventList.size());
                    }
                }
                if(itTask.WhoId !=null) {
                    if(String.valueOf(itTask.WhoId).startsWith(contactidPrifix) || String.valueOf(itTask.WhoId).startsWith(LeadIdPrefix)){
                        taskEventList.add(new eEventTTask(itTask.Subject,itTask.Id,itTask.OwnerId,itTask.ActivityDate, itTask.LastModifiedDate,itTask.who.Name,itTask.what.Name,itTask.Description, itTask.owner.Name,itTask.CreatedDate));
                        system.debug('$$$$taskEventList'+taskEventList);
                        system.debug('****taskEventList.size()'+taskEventList.size());
                    }
                }
                
                // taskEventList.sort();
            }
        }
    }
    public PageReference cancel() {
        PageReference ldPage = new PageReference('/'+accountId);
        ldPage.setRedirect(true);
        return ldPage;
    }  
    public class eEventTTask {
        public String subject { get; set; }
        public Id whatId { get; set; }
        public Id ownerId { get; set; }
        public string whoid1{get;set;}
        public string whatIdName{get;set;}
        public string Description{get;set;}
        public string OwnerName{get;set;}
        public Date activityDate { get; set;}
        public DateTime lastModified { get; set;}
        public DateTime createdDate {get;set;}
        public eEventTTask(String sub, Id wht, Id own, Date act, Datetime lmd, String whoname, String whatName,string Description,string OwnerName,dateTime cd) {
            this.subject = sub;
            this.whatId = wht;
            this.whoId1  =whoname;
            this.whatIdName = whatName;
            this.description =Description;
            this.OwnerName = OwnerName;
            this.createdDate = cd;
            this.ownerId = own;
            this.activityDate = act;
            this.lastModified = lmd;
        }
    }
}



VF page : 

<apex:page controller="ShowAllSObjectActivity" sidebar="false" standardStylesheets="false">
    <apex:sectionHeader title="Account  Activity History1"/>
    <script>
  $(document).ready(function()
  {
    $("[id$='accsTable']").tablesorter({theme: 'blue', dateFormat : "dd/mm/yyyy"});
  });
</script>
    <apex:form >
        <html xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" lang="en">
            <head>
                <meta charset="utf-8" />
                <meta http-equiv="x-ua-compatible" content="ie=edge" />
                <meta name="viewport" content="width=device-width, initial-scale=1" />
                <!-- <apex:stylesheet value="{!URLFOR($Resource.SLDS103, 'assets/styles/salesforce-lightning-design-system-vf.min.css')}" />-->
                <apex:includeScript
                                    value="//cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.17.8/js/jquery.tablesorter.min.js" />
            </head>
            <div class="slds">
                <div class="slds-grid slds-wrap slds-grid--pull-padded">
                    <div class="slds-p-horizontal--small slds-size--1-of-1">
                        <center>
                            <apex:commandButton action="{!cancel}" value="cancel" styleClass="slds-button slds-button--neutral" status="actStatusId" />
                        </center>
                    </div>
                    <div class="slds-p-horizontal--small slds-size--1-of-1">
                        
                        <apex:repeat value="{!taskEventList}" var="oSobject" id="accsTable">
                            <div class="slds-border--top">
                                <div class="slds-form-element">
                                    <div class="slds-form-element__control slds-has-divider--bottom">
                                        <!--<b> <span class="slds-form-element__addon">Subjectspan>b>-->
                                        <span class="slds-form-element__static">
                                            <apex:outputText value="{!oSobject.Subject}" />
                                        </span>
                                    </div>
                                </div>
                                <div class="slds-form-element">
                                    <div class="slds-form-element__control slds-has-divider--bottom">
                                        <b> <span class="slds-form-element__addon"/>Namespan></b>
                                        <span class="slds-form-element__static">
                                            <apex:outputText value="{!oSobject.whoId1}" />
                                            {!oSobject.whoId1}
                                        </span>
                                    </div>
                                </div>
                                <div class="slds-form-element">
                                    <div class="slds-form-element__control slds-has-divider--bottom">
                                        <b> <span class="slds-form-element__addon"/>Related Tospan></b>
                                        <span class="slds-form-element__static">
                                            <apex:outputText value="{!oSobject.whatIdName}" />
                                        </span>
                                    </div>
                                </div>
                                <div class="slds-form-element">
                                    <div class="slds-form-element__control slds-has-divider--bottom">
                                        <b> <span class="slds-form-element__addon"/>Due Datespan></b>
                                        <span class="slds-form-element__static">
                                            <apex:outputText value="{!oSobject.activityDate}"/>
                                        </span>
                                    </div>
                                </div>
                                <div class="slds-form-element">
                                    <div class="slds-form-element__control slds-has-divider--bottom">
                                        <b> <span class="slds-form-element__addon"/>Assigned Tospan></b>
                                        <span class="slds-form-element__static">
                                            <apex:outputText value="{!oSobject.OwnerName}" />
                                        </span>
                                    </div>
                                </div>
                                <div class="slds-form-element">
                                    <div class="slds-form-element__control slds-has-divider--bottom">
                                        <b> <span class="slds-form-element__addon"/>Last Modified Date/Timespan></b>
                                        <span class="slds-form-element__static">
                                            <apex:outputText value="{!oSobject.lastModified}"/>
                                        </span>
                                    </div>
                                </div>
                                <div class="slds-form-element">
                                    <div class="slds-form-element__control slds-has-divider--bottom">
                                        <b> <span class="slds-form-element__addon"/>Created Date></b>
                                        <span class="slds-form-element__static">
                                            <apex:outputText value="{!oSobject.Createddate}"/>
                                        </span>
                                    </div>
                                </div>
                                <div class="slds-form-element">
                                    <div class="slds-form-element__control slds-has-divider--bottom">
                                        <b> <span class="slds-form-element__addon"/>Commentsspan></b>
                                        <span class="slds-form-element__static">
                                            <apex:outputText value="{!oSobject.Description}" />
                                        </span>
                                    </div>
                                </div>
                            </div>
                            <hr style="background-color:black;"/>
                        </apex:repeat>
                    </div>
                    
                </div>
            </div>
        </html>
    </apex:form>
</apex:page>



Thanks.
Dinesh RDinesh R
Hi Prosenjit,

Do you have any idea to resolve the issue?Please let me know?
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Dinesh , 

Remove the inner class from the main controller class, 

and create a new class as a Wrapper class and refer it in the main controller class, 
 
global class eEventTTask implements Comparable {
        public String subject { get; set; }
        public Id whatId { get; set; }
        public Id ownerId { get; set; }
        public string whoid1{get;set;}
        public string whatIdName{get;set;}
        public string Description{get;set;}
        public string OwnerName{get;set;}
        public Date activityDate { get; set;}
        public DateTime lastModified { get; set;}
        public DateTime createdDate {get;set;}
        public eEventTTask(String sub, Id wht, Id own, Date act, Datetime lmd, String whoname, String whatName,string Description,string OwnerName,dateTime cd) {
            this.subject = sub;
            this.whatId = wht;
            this.whoId1  =whoname;
            this.whatIdName = whatName;
            this.description =Description;
            this.OwnerName = OwnerName;
            this.createdDate = cd;
            this.ownerId = own;
            this.activityDate = act;
            this.lastModified = lmd;
        }
		
		// Implement the compareTo() method
		global Integer compareTo(Object compareTo) {
			eEventTTask compareToEventTask = (eEventTTask)compareTo;
			if (activityDate == compareToEventTask.activityDate) {
				return 0;
			}			
			if (activityDate > compareToEventTask.activityDate) {
				return 1;
			}			
			return -1;        
		}
    }

After that uncomment this line,
// taskEventList.sort();

Please let me know if any problem arises,

Thanks, 
Prosenjit.


 
This was selected as the best answer
Dinesh RDinesh R
Hey Prosenjit.

Awesome, it's working for the expected.


Thanks for your valuable help.
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Dinesh , 

Please mark it as a best answer if it solves your problem. 

Thanks, 
Prosenjit
Dinesh RDinesh R
Hi  Prosenjit,
I already implemented same logic but the only one issue is wrapper class using inside the main controller class. Do you think this is an issue? I am not understanding. But, Issue is resolved.
If you clearly understand or know the details on the issue please explain to me.



Thanks 
Dinesh
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Dinesh , 
Two possibilities can be there, 
  1. You have missed to implement the Comparable interface.
  2. There was some wrong code in compareTo method
Parting the wrapper class just a bes practise.
Thanks,
Prosenjit
Dinesh RDinesh R
Hi Prosenjit

I need to display the data in Descending order. SO how to use the sort method in the Descending order. 

Thanks
Dinesh RDinesh R
Hi Prosenjit,

Facing some issue on this code. It's working only for 1 or 2 task or event only but adding more than one event the sorting order is not working properly. Also, I need to sort in descending order.
Dinesh RDinesh R
Hi Prosenjit,
Do you have any idea or any suggestion to fix the issue?Please let me know if able to help.

Thanks
dinesh
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Dinesh, 
I have made the reference field as activitydate. If you need you can change it to other field to vaidalte like createddate and to sort them descending order, you can check with this code,
 
// Implement the compareTo() method
		global Integer compareTo(Object compareTo) {
			eEventTTask compareToEventTask = (eEventTTask)compareTo;
			if (activityDate == compareToEventTask.activityDate) {
				return 0;
			}			
			if (activityDate < compareToEventTask.activityDate) {
				return 1;
			}			
			return -1;        
		}

Just change the > sign to < it will change the order. 

Thanks,
Prosenjit 
Dinesh RDinesh R
Hi Prosenjit

yes, I already fixed that issue. Anyway thanks for your help.

Thanks