1

I have a wrapper of Tasks and Events that I want to sort by: ActivityDate then by LastModifiedDate then by CreatedDate (all in descending order)

My initial thought was to do the comparable interface but because it is two different objects with three filters I got a bit stuck. Would I need to do multiple comparable methods?

My second thought was loop through the wrapper and compare the dates against each other and then add them back into a newly sorted wrapper list. My main concern with this was having multiple nested loops.

public class wrapActivity implements Comparable{

public Date sActivityDate { get; set; }
public DateTime sActivityCreatedDate { get; set; }
public DateTime sLastModifiedDate { get; set; }
public String sActivityOwner { get; set; }
public String sId { get; set; }
public String sActivityObject { get; set; }
public String sActivitySubject { get; set; }
public String sActivityComments { get; set; }
public String sActivityType {get; set; }

 public wrapActivity(String id, Date activityDate, DateTime activityCreatedDate, DateTime activityLastModifiedDate, String activityOwner, String activityObject, String activitySubject, String activityComments, String activityType ){
    this.sId = id;
    this.sActivityComments = activityComments;
    this.sActivityCreatedDate = activityCreatedDate;
    this.sActivityDate = activityDate;
    this.sActivityObject = activityObject;
    this.sActivityOwner = activityOwner;
    this.sActivitySubject = activitySubject;
    this.sActivityType = activityType;
    this.sLastModifiedDate = activityLastModifiedDate;
}

public Integer compareTo(Object compareTo){
    wrapActivity wrap = (wrapActivity) compareTo;
    if (sActivityDate == wrap.sActivityDate) {
        return 0;
    }

    if (sActivityDate > wrap.sActivityDate) {
        return 1;
    }
    
    return -1; 
   // is it possible to do another compareTo() method in here for LastModifiedDate and CreatedDate?
}
}

1 Answer 1

3

It's really no more complicated than a few extra if statements.

if(sActivityDate > wrap.sActivityDate) {
  return 1;
}
if(sActivityDate < wrap.sActivityDate) {
  return -1;
}
if(sLastModifiedDate > wrap.sLastModifiedDate) {
  return 1;
}
if(sLastModifiedDate < wrap.sLastModifiedDate) {
  return -1;
}
if(sActivityCreatedDate > wrap.sActivityCreatedDate) {
  return 1;
}
if(sActivityCreatedDate < wrap.sActivityCreatedDate) {
  return -1;
}
return 0;

If we check < and >, and both return false, then we know that the values must be equal. This is a helpful technique to remember: if we've checked all other possibilities, we can infer additional logical conclusions. This comes easier with practice.

However, in case it matters, < and > return false if either side is null, so you may need to account for that in your code somewhere, otherwise our logical assumptions won't hold, and the sort order would act weird in the case of null values.

You must log in to answer this question.

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