0

I have three objects, the Professional_Experience__c, Assignment__c, and Project__c. Professional Experience has two child (Assignment__c, and Project__c). Then the Assignment__c has also a child (Project__c).

public class profExpWrapper{
    public Id peId {get;set;}
    public Professional_Experience__c peWrap {get;set;}
    public List<Project__c> projWrap {get;set;} //list of standalone project
    public List<assignmentWrapper> assignmentListWrapper1 {get;set;} //list of assignmentWrapper
}

public class assignmentWrapper{
    public Id assignmentId {get;set;}
    public Assignment__c assignmentWrap {get;set;}
    public List<Project__c> projectforassignmentWrap {get;set;} //list of project per assignment
}

I want to compare the record of Assignment__c and Project__c thru date fields so that if the Assignment record is more recent, it will show first. So that when I go to Professional_Experience__c record, the 2 child will be arranged / sort by the date field. How can I achieve this using comparable?

1 Answer 1

0

Your wrapper class should contain the date field as variable to compare.

I am writing the method for sorting the Assignment__c using comparable. You need to have the start date as wrapper variable to sort the wrapper list based on that date. Lets assume that Date wdate is a wrapper variable which stores the date value. Now to compare based on this variable your wrapper should implement comparable -

public class assignmentWrapper implements comparable

Now your sorting method is as follows -

public Integer compareTo(Object objToCompare) {
    assignmentWrapper that = (assignmentWrapper) objToCompare;

        if (this.wdate < that.wdate) 
            return 1;
        else if (this.wdate > that.wdate) 
            return -1;
        else 
            return 0;

}

Now you can sort the wrapper by using the following statement -

<assignmentWrapper instance>.sort();

this will return the wrapper data list in a descending way. You can do that in ascending way by replacing 1 and -1;

Similarly you can sort the project wrapper.

You must log in to answer this question.

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