0

I have a custom object called Insurance which has a Start Date field. I am planning to use bubble sort to sort the records based on the Start Date. This is what I have tried so far and got confused as to how to return all the records after sorting the dates.

 //Sorting logic 
          List<Insurance__c> listOfRecords = new List<Insurance__c>();
        integer iterationSize = listOfRecords.size();
        for(Insurance__c iList1 : insuranceList){
            listOfRecords.add(iList1);
        }
        
        
        
        for(integer y=0; y<listOfRecords.size();y++){
            boolean swapped = false;
            for(integer x =0; x< iterationSize; x++)
            {
                if(x+1 == listOfRecords.size()) break;
                if(listOfRecords.get(x).IAR_Start_Date__c < listOfRecords.get(x+1).IAR_Start_Date__c)
                {
                    Insurance__c swapVal = listOfRecords.get(x);
                    swapped = true;
                    listOfRecords.set(x, listOfRecords.get(x+1));
                    listOfRecords.set(x+1, swapVal);
                }
            }
            iterationSize--;
            if(!swapped){y= listOfRecords.size();}
            
        }  

Edit 1: Insurance Wrapper Class to sort the records based on the IAR Start Date

global class InsuranceWrapper implements Comparable {
    
    public Insurance__c ins;
    public Date insuranceDate;
    
    public InsuranceWrapper(Insurance__c i){
        ins = i;
        insuranceDate = i.IAR_Start_Date__c;
    }
    
      // Compare Insurance based on the Insurance Date.
    global Integer compareTo(Object compareTo) {
        // Cast argument to OpportunityWrapper
        InsuranceWrapper compareToins = (InsuranceWrapper)compareTo;

        // The return value of 0 indicates that both elements are equal.
        Integer returnValue = 0;
        if (insuranceDate > compareToins.insuranceDate) {
            // Set return value to a positive value.
            returnValue = 1;
        } else if (insuranceDate < compareToins.insuranceDate) {
            // Set return value to a negative value.
            returnValue = -1;
        }

        return returnValue;       
    }
    
    

} 
12
  • 2
  • Hi, I don’t want to use any wrapper class rather need to implement the above logic using bubble sort. Feb 21 at 15:32
  • @RubenDG I have made changes to the sorting logic but still it is not sorting. Do you know hat could be the issue? Feb 21 at 15:55
  • 2
    @Krishnamurthy by the way, do not use bubble sort in production code. It will time out for even moderately sized lists. Bubble sort's efficiency approaches O(0.5(n^2)) records with this version of the sorting algorithm, while the built-in list.sort method approaches O(log2) efficiency. At 300 records in the list, it would take 450,000 comparison operations, while list.sort can do it in just a couple thousand.
    – sfdcfox
    Feb 21 at 19:59
  • 1
    @Krishnamurthy you called it ins apparently. I was just providing a generic algorithm. It's the inverse of putting the records into the wrapped list to begin with.
    – sfdcfox
    Feb 22 at 5:10
3

Your code is sorting the list you created in place by swapping values between pairs of indexes. So you should return listOfRecords.

But the early code is wrong; it leaves iterationSize always zero. So move that assignment to after listOfRecords has values added.

A Comparable wrapper is a reliable and common approach. While it is tedious to code it isn't complicated and you benefit from an already proven sort method. Why don't you want to use that approach?

PP

The wrapper approach could be e.g.:

public class Sorter {

    private class Wrapper implements Comparable {

        Insurance__c record;

        Wrapper(Insurance__c record) {
            this.record = record;
        }

        public Integer compareTo(Object o) {

            Insurance__c that = (Insurance__c) o;

            Date thisValue = this.record.IAR_Start_Date__c;
            Date thatValue = that.record.IAR_Start_Date__c;            

            // Assumes dates never null
            if (thisValue < thatValue) return -1;
            else if (thisValue > thatValue) return 1;
            else return 0;
        }
    }
        
    public static List<Insurance__c> sort(List<Insurance__c> records) {

        // Wrap
        List<Wrapper> wrappers = new List<Wrapper>();
        for (Insurance__c record : records) wrappers.add(new Wrapper(record));

        // Use the platform's built in sort algorithm
        wrappers.sort();

        // Unwrap
        List<Insurance__c> results = new List<Insurance__c>();
        for (Wrapper wrapper : wrappers) results.add(wrapper.record);

        return results;
    }
}
6
  • Thanks Keith. I have already figured it out. I haven’t used wrapper classes. May I know what’s it? Could you share any resources where I can learn step by step? Feb 21 at 16:58
  • @Krishnamurthy, Oe example here salesforce.stackexchange.com/questions/48000/… but just Google for more. For sorting, the point is to add another method - compareTo - via a class that holds a reference to the class being sorted.
    – Keith C
    Feb 21 at 23:30
  • In my case I would need to compare the dates and return the records as a whole. How to achieve the same? This is super new to me. Feb 22 at 0:33
  • @Krishnamurthy See the PS I added.
    – Keith C
    Feb 23 at 15:28
  • 1
    Yes: set up some data, call your sort, then assert the results of the sort.
    – Keith C
    Feb 23 at 15:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.