How to use Comparable Interface in Apex with a real-life example!

How to use Comparable Interface in Apex with a real-life example!

Business Example:

I would like to give an example from one of my important clients, Trio Cab. They have been assisting passengers with transferring from point A to point B. For instance, when you travel abroad and need to get to a hotel after arriving at the airport, Trio Cab helps people reach their destination.

However, they do not want to assign reservations manually to the suppliers or drivers. They aim to automate the process, matching reservation owners with vehicles based on capacity and availability. Let's explore how we can achieve this using Apex!

First things first, let's prepare a class for sorting.

public class AccountCapacityComparator implements Comparable {
        public Id accountId;
        public Decimal remainingCapacity;
        public Decimal totalCapacity;
    
        public AccountCapacityComparator(Id accountId, Decimal remainingCapacity, Decimal totalCapacity) {
            this.accountId = accountId;
            this.remainingCapacity = remainingCapacity;
            this.totalCapacity = totalCapacity;
        }
    
        public Integer compareTo(Object objToCompare) {
            AccountCapacityComparator toCompare = (AccountCapacityComparator)objToCompare;
            
            if (remainingCapacity == toCompare.remainingCapacity) {
                if (totalCapacity == toCompare.totalCapacity){
                    return 0;
                }
                if (totalCapacity > toCompare.totalCapacity){
                    return -1;
                }
                return 1;
            }
            if (remainingCapacity > toCompare.remainingCapacity){
                return -1; // Switched this to prioritize most empty
            }
            return 1;
        }
    }        

Then, you can use below code block below wherever you want such as in Trigger!

Fetch all accounts and their capacities;

List<Account> allAccounts = 
[
SELECT Id, Capacity__c, TotalReservation__c
FROM Account
];        

For each account, compute the remaining capacity for the given day

List<AccountCapacityComparator> accountCapacities = new List<AccountCapacityComparator>();
for(Account acc : allAccounts) {
    Decimal currentNoReservations = acc.TotalReservation__c;
    Decimal remainingCapacity = acc.Capacity__c - currentReservations;
    accountCapacities.add(new AccountCapacityComparator(acc.Id, remainingCapacity, acc.Capacity__c));
}        

Sort the accounts by their remaining capacities and then by total capacities in descending order.

AccountCapacityComparator class provides the way of ordering asc or desc.

accountCapacities.sort();        

Check and return the "accountId" with the most remaining capacity and highest total capacity! The first item on the list will be the most suitable one!

for(AccountCapacityComparator accCapacity : accountCapacities) {
    if(accCapacity.remainingCapacity > 0) {
        return accCapacity.accountId; 
    }
}        

要查看或添加评论,请登录

社区洞察

其他会员也浏览了