0

This is the method

@AuraEnabled
public static list<documentEntity> GetDocumentsInfo(){
    try{
        List<DocumentType__c> documentList = DocumentType__c.getall().values();
        list<documentEntity> result = new list<documentEntity>();
        for (DocumentType__c i : documentList){
            result.add(new documentEntity(i.displayName__c, i.APIName__c, false));
        }
        return result;
    } catch (Exception e){
        return null;
    }
}

This is the constructor of documentEntity

public class documentEntity{
    @AuraEnabled
    public String documentName;
    @AuraEnabled
    public String documentAPIName;
    @AuraEnabled
    public Boolean documentSelected;
    public documentEntity(String documentName, String documentAPIName, Boolean 
    documentSelected){
        this.documentName = documentName;
        this.documentSelected = documentSelected;
        this.documentAPIName = documentAPIName;
    }
}
1
  • It's worth noting that sorting in Apex is rather archaic. You might want to just do sorting on the client instead. It's much faster and easier to write.
    – sfdcfox
    Commented Jul 6, 2021 at 0:03

1 Answer 1

3

In order to use list.sort() with custom types, they must implement Comparable Interface, that define only one method: Integer compareTo(Object compareTo).

foo.compareTo(bar) should return the following values:

  • 0 if the objects are equal
  • a positive value if foo is greater than bar
  • a negative value if foo is less than bar

Here's an example that will sort a List<DocumentEntity> in ascending order based on the comparison of documentName.

public class DocumentEntity implements Comparable {
    @AuraEnabled
    public String documentName;
    @AuraEnabled
    public String documentAPIName;
    @AuraEnabled
    public Boolean documentSelected;

    public documentEntity(String documentName, String documentAPIName, Boolean documentSelected){
        this.documentName = documentName;
        this.documentSelected = documentSelected;
        this.documentAPIName = documentAPIName;
    }

    public Integer compareTo(Object compareTo) {
        DocumentEntity compareToEntity = (DocumentEntity) compareTo;
        if (documentName == compareToEntity.documentName) {
            return 0;
        }
        if (documentName > compareToEntity.documentName) {
            return 1;
        }
        return -1;
    }
}

Then calling result.sort() sort your list.

The comparison can have multiple steps, i.e. if you want to compare first by documentName, then by documentAPIName:

public Integer compareTo(Object compareTo) {
    DocumentEntity compareToEntity = (DocumentEntity) compareTo;
    if (documentName == compareToEntity.documentName) {
        if (documentAPIName == compareToEntity.documentAPIName) {
            return 0;
        }
        if (documentAPIName > compareToEntity.documentAPIName) {
            return 1;
        }
        return -1;
    }
    if (documentName > compareToEntity.documentName) {
        return 1;
    }
    return -1;
}

You must log in to answer this question.

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