function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sheena QueridoSheena Querido 

Sort table on PDF Visualforce

I have a controller for my custom object that is currently sorting columns in my table. However, it's sorting on every column. 
public class ExhibitAController {
    public Customer_Pricing__c cust {get;set;}
    public List<Customers_Product_Code__c> customerproducts {get;set;}
        
    public ExhibitAController(ApexPages.StandardController stdController){
    
        this.cust = (Customer_Pricing__c)stdController.getRecord();

        customerproducts  = [select Description_for_Invoice__c,TSDF_Approval_Number__c,Caveats__c,Category__c,Container_Size__c,UM__c,Price__c from Customers_Product_Code__c where Customer_Pricing__c =:cust.Id];
    
            customerproducts.sort();

    }
    

    
}

How do I adjust the code to only sort on columns/fields that I specify? 
Best Answer chosen by Sheena Querido
Ashish KumarAshish Kumar
Hi Sheena, Please use ORDER BY After Where clause. Below is the usage of ORDER BY with Where clause: SELECT Name FROM Account WHERE industry = 'media' ORDER BY BillingPostalCode ASC NULLS LAST LIMIT 125 Regards, Ashish

All Answers

Alain CabonAlain Cabon
The default rules of sorting for List cannot be changed with parameters like: mylist.sort(param1,param2,...).

You need a wrapper class and to adapt your Visualforce page using this wrapper class.

Custom Sort Order of sObjects: To implement a custom sort order for sObjects in lists, create a wrapper class for the sObject and implement the Comparable interface. The wrapper class contains the sObject in question and implements the compareTo method, in which you specify the sort logic.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_list_sorting_sobject.htm
 
public class CustomerPricingWrapper implements Comparable {

    public Customer_Pricing__c cust {get;set;}  
    
    // Constructor
    public CustomerPricingWrapper(Customer_Pricing__c cp) {
        cust = cp;
    }
    
    // Compare Customer Pricing based on the Container_Size__c.
    public Integer compareTo(Object compareTo) {
        // Cast argument to CustomerPricingWrapper 
        CustomerPricingWrapper compareToCust = (CustomerPricingWrapper)compareTo;
        
        // The return value of 0 indicates that both elements are equal.
        Integer returnValue = 0;
        if (cust.Container_Size__c > compareToCust.cust.Container_Size__c) {
            // Set return value to a positive value.
            returnValue = 1;
        } else if (cust.Container_Size__c < compareToCust.cust.Container_Size__c) {
            // Set return value to a negative value.
            returnValue = -1;
        }        
        return returnValue;       
    }
}

and therefore:

public CustomerPricingWrapper custw {get;set;}  have to be used in your VFP instead of Customer_Pricing__c cust.

custw.cust.Container_Size__c

Regards
Ashish KumarAshish Kumar
Hi Sheena,

Why don't you use ORDER BY in you SOQL. It will help you in sorting only the fields you want to sort.

Read here for more info : https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_orderby.htm

Regards
Alain CabonAlain Cabon
and don't forget to remove: customerproducts.sort(); if you use "SELECT ... ORDER BY columns" directly instead.
Ashish KumarAshish Kumar
Hi Sheena, Please use ORDER BY After Where clause. Below is the usage of ORDER BY with Where clause: SELECT Name FROM Account WHERE industry = 'media' ORDER BY BillingPostalCode ASC NULLS LAST LIMIT 125 Regards, Ashish
This was selected as the best answer
Sheena QueridoSheena Querido
Hi Ashish and Alain,

Thank you for your input! I was able to get the controller to work once I removed the previous customerproducts.sort();