3

I just want to ask if comparing of each fields on separate object is possible in order to show first the most recent.

Example, I have Object A and Object B, and Object A has Hired_Date__c , and Object B has Hired_Date__c also,

is it possible to sort them in a way of comparing using comparable.

if(Object A.Hired_Date__c > Object B.Hired_Date__c){
   return 1;
}

else if (Object A.Hired_Date__c < Object B.Hired_Date__c){
   return -1;
}

else {
    return 0;
}

so that I could display on visualforce page the arrangement of Object A1 Object B2, so on . . or how could I achieve to sort this 2 different records?

2 Answers 2

6

It is possible to write a Comparable wrapper class that can work with various SObject types and fields:

public class DateFieldComparable implements Comparable {

    private SObject sob;
    private SObjectField field;

    public DateFieldComparable(SObject sob, SObjectField field) {
        this.sob = sob;
        this.field = field;
    }

    public Integer compareTo(Object o) {

        DateFieldComparable that = (DateFieldComparable) o;

        Date thisValue = (Date) this.sob.get(field);
        Date thatValue = (Date) that.sob.get(that.field);

        if (thisValue < thatValue) return -1;
        else if (thisValue > thatValue) return 1;
        else return 0;
    }

    public SObject getSObject() {
        return sob;
    }
}

that can then be used like this:

// Wrap and sort
DateFieldComparable[] fcs = new DateFieldComparable[] {};
for (Project__c p : projects) {
    fcs.add(new DateFieldComparable(p, Project__c.MyDateField__c));
}
for (assignment__c : assignments) {
    fcs.add(new DateFieldComparable(a, Assignment__c.OtherDateField__c));
}
fcs.sort();

// Pull back out the sorted objects
SObject[] sobs = new SObject[] {};
for (FieldComparable fc : fcs) {
    sobs.add(fc.getSObject());
}

This code is using the SObjectField token to represent a field which has the benefit that the compiler checks that the field exists. But if it is more convenient, you can use a string that holds the field API name because there is an SObject.get available for both types.

PS

Changed to a specific data type per sfdcfox's comment.

7
  • +1 This code won't compile, though, because you can't actually use inequality operators on Object, so they'd have to be prepared to use a concrete data type (e.g. Date), or build some sort of plug-in interface.
    – sfdcfox
    Commented Apr 21, 2017 at 8:44
  • @sfdcfox Thanks, had a bad feeling about that but didn't try it... Changed code to Date type.
    – Keith C
    Commented Apr 21, 2017 at 9:02
  • Hi @KeithC thank you very much! for confirmation only, did you changed it already the date u've trying to say from the above code?
    – boy george
    Commented Apr 24, 2017 at 0:53
  • Hi @KeithC I'm currently suffering this error. ''Project__c.Start_Date__c does not belong to SObject type Assignment__c" when I'm trying to make a for loop on assignment and project.
    – boy george
    Commented Apr 24, 2017 at 7:58
  • @RonaldPascual A field token for the Assignment__c object would be Assignment__c.xxx__c: if the date field there has the API name Start_Date__c then it would be Assignment__c.Start_Date__c.
    – Keith C
    Commented Apr 24, 2017 at 8:36
2

Yes, that should be no problem: This is exactly what the Comparable interface is for.

global class Object implements Comparable { // Change this "Object" to your object name
    Date hiredDate;

    public Object(Date hd){ // Change this "Object" to your object name
        hiredDate = hd;
    }

    global Integer compareTo(Object compareTo) { // DO NOT change this "Object" to your object name
        Object compareToObject = (Object) compareTo;
        // Change this "Object" to your object name 
        // This line is important, make sure you cast to your custom object

        if(hiredDate == compareToObject.Hired_Date__c) return 0;
        if(hiredDate > compareToObject.Hired_Date__c) return 1;
        return -1;
    }
}

Please refer to the documentation for more examples & explanations: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_comparable.htm

1
  • Hi @Koen Wesselman , I think my question is not cleared, so about that, but I'll give you more detailed, I have Professional_Skills__c, and inside the professional skills, I have 2 different record, project__c and assignment__c, and the project has date field, and assignment also has. so that inside on my professional skills list will be the mix project and assignment record. example i have 3 assignment and 2 project. will result like this. 1. assignment(March 2017) 2. project(February 2017) 3. assignment1(January 2017) 4. project(December 2016) 5. assignment3(November 2016)
    – boy george
    Commented Apr 21, 2017 at 6:54

You must log in to answer this question.

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