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
Admin User 10568Admin User 10568 

Creating Standard Controller Extension to Sort List Ascending

Hi, 

Hopeing someone can help me here. 

I would like to filter results in a pageBlockTable in ascending order.

I've created a standard controller extension: 

public with sharing class ProjectONSExtension
{
    private final Id acctId;
    public List<REPRO__Project__c> oncs { get; set; }

    public ProjectONSExtension(ApexPages.StandardController stdController)
    {
        acctId = stdController.getId();
        oncs = Database.query('SELECT Name,REPRO__Street__c, REPRO__City__c, (SELECT Id,name,Property_Level__c,Price_List_Property_Name__c,REPRO__Type__c,REPRO__Bdr__c,REPRO__Bth__c,REPRO__Study__c,REPRO__Internal_Size__c,REPRO__External_Size__c,REPRO__Car__c,REPRO__List_Price__c,REPRO__Status__c from REPRO__Properties__r where REPRO__Project__c =:acctId order by Price_List_Property_Name__c )' +
                ' FROM REPRO__Project__c ' 
   );             
    }
}

It runs without an error. But it didn't order the pageBlockTable results in ascending order. Boo! Any thoughts?

Here is the visual page for reference:

<apex:page standardController="REPRO__Project__c" extensions="ProjectONSExtension" renderAs="pdf" showHeader="false" sidebar="false" >

    <apex:pageBlock >
     
          <h1 style= "text-align: center;">{!REPRO__Project__c.name}</h1>
        <p style= "text-align: center;">{!REPRO__Project__c.REPRO__Street__c}, {!REPRO__Project__c.REPRO__City__c} <br/> Price List as of {! TODAY()} </p>
   
        <apex:pageBlockTable value="{!REPRO__Project__c.REPRO__Properties__r}" var="custom" align="center" cellpadding="1" border="2"  style="font-weight: bold; text-align: center; ">
              <apex:column value="{!custom.Property_Level__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />
              <apex:column value="{!custom.Price_List_Property_Name__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />
              <apex:column value="{!custom.REPRO__Type__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />   
              <apex:column value="{!custom.REPRO__Bdr__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />
              <apex:column value="{!custom.REPRO__Bth__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />
              <apex:column value="{!custom.REPRO__Study__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />
              <apex:column value="{!custom.REPRO__Internal_Size__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; "  />
              <apex:column value="{!custom.REPRO__External_Size__c}"  style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />
              <apex:column value="{!custom.REPRO__Car__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />
              <apex:column value="{!custom.REPRO__List_Price__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />
              <apex:column value="{!custom.REPRO__Status__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />
            </apex:pageBlockTable>

Any help would be greatly appreciated :)
Best Answer chosen by Admin User 10568
ANUTEJANUTEJ (Salesforce Developers) 
 So I tried checking and I see that there is another example in a similar way but with an extension can you try checking it, below is the link:

>> https://developer.salesforce.com/forums/?id=906F0000000BVX2IAO

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi there,

>> https://help.salesforce.com/articleView?id=000328724&type=1&mode=1

I found the above link to the help article that has an implementation of showing records in a sorting order can you try checking the above link once.

I am adding the snippet present for quick reference :
 
APEX CODE:

public class PageBlockTableSortingCon {
 
   private List<Account> accounts;
   private String sortDirection = 'ASC';
   private String sortExp = 'name';

   public String sortExpression
   {
     get
     {
        return sortExp;
     }
     set
     {
       //if the column is clicked on then switch between Ascending and Descending modes
       if (value == sortExp)
         sortDirection = (sortDirection == 'ASC')? 'DESC' : 'ASC';
       else
         sortDirection = 'ASC';
       sortExp = value;
     }
   }

 public String getSortDirection()
 {
    //if not column is selected
    if (sortExpression == null || sortExpression == '')
      return 'ASC';
    else
     return sortDirection;
 }

 public void setSortDirection(String value)
 {  
   sortDirection = value;
 }
 
   public List<Account> getAccounts() {
       return accounts;
   }


   public PageReference ViewData() {
       //build the full sort expression
       string sortFullExp = sortExpression  + ' ' + sortDirection;
      
       //query the database based on the sort expression
       accounts = Database.query('Select id, Name, BillingCity, BillingCountry, Phone from Account order by ' + sortFullExp + ' limit 1000');
       return null;
   }

}


VF Page:

<apex:page controller="PageBlockTableSortingCon" tabStyle="Account">
<apex:sectionHeader title="Accounts List with Sorting"></apex:sectionHeader>
<apex:form >
<apex:pageBlock title="" id="pageBlock">
    <apex:pageBlockButtons location="top">
      <apex:commandButton value="View" action="{!ViewData}" id="theButton" rerender="pageBlock"></apex:commandButton>
    </apex:pageBlockButtons>
    <apex:pageMessages ></apex:pageMessages>
    <apex:pageBlockTable value="{!accounts}" var="a" rendered="{!NOT(ISNULL(accounts))}">
       <apex:column >
         <apex:facet name="header">   
           <apex:commandLink action="{!ViewData}" value="Account Name{!IF(sortExpression=='name',IF(sortDirection='ASC','▼','▲'),'')}" id="cmdSort">
             <apex:param value="name" name="column" assignTo="{!sortExpression}" ></apex:param>
           </apex:commandLink>
         </apex:facet>
         <apex:outputLink value="/{!a.Id}" target="_blank">{!a.Name}</apex:outputLink>
       </apex:column>
       <apex:column value="{!a.Phone}">
         <apex:facet name="header">
           <apex:commandLink action="{!ViewData}" value="Phone{!IF(sortExpression=='Phone',IF(sortDirection='ASC','▼','▲'),'')}">
             <apex:param value="Phone" name="column" assignTo="{!sortExpression}" ></apex:param>
           </apex:commandLink>
         </apex:facet>
       </apex:column>
        <apex:column value="{!a.BillingCity}">
          <apex:facet name="header">
           <apex:commandLink action="{!ViewData}" value="Billing City{!IF(sortExpression=='BillingCity',IF(sortDirection='ASC','▼','▲'),'')}">
             <apex:param value="BillingCity" name="column" assignTo="{!sortExpression}" ></apex:param>
           </apex:commandLink>
         </apex:facet>
       </apex:column>
        <apex:column value="{!a.BillingCountry}">
          <apex:facet name="header">
           <apex:commandLink action="{!ViewData}" value="Billing Country{!IF(sortExpression=='BillingCountry',IF(sortDirection='ASC','▼','▲'),'')}">
             <apex:param value="BillingCountry" name="column" assignTo="{!sortExpression}" ></apex:param>
           </apex:commandLink>
         </apex:facet>
       </apex:column>
     
    </apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Admin User 10568Admin User 10568
Hi @ANUTEJ,

Thanks for the URL and the explanation. Super helpful for creating filters on a table and a custom controller. 

I have a requirement for using a standard controller and an extension to perform the ascending order request. The page will be used as part of a custom button on the object. 

I need help to understand why my code has no error yet doesn't pass the ascending request in the controller extension. 

It would be amazing if you or someone could help me understand what changes need to happen to make it happen. 

Many thanks :)
ANUTEJANUTEJ (Salesforce Developers) 
 So I tried checking and I see that there is another example in a similar way but with an extension can you try checking it, below is the link:

>> https://developer.salesforce.com/forums/?id=906F0000000BVX2IAO

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
This was selected as the best answer
Admin User 10568Admin User 10568
Hi @ANUTEJ, 

Thanks for your thoughtful sharing.

This was okay for a reference. >> https://developer.salesforce.com/forums/?id=906F0000000BVX2IAO

I worked it out with trial and error.

Two main issues/changes.

One. I was treating the SOQL as if I needed to traverse child to parent and same with creating the method. Updated to reference the object extension and the SOQL to reflect this. It works now. Two. Within the visual page I needed to reference the method in the table. 

It works now. 

Many thanks for helping with the reference URL.
Amanda Carolina PistolatoAmanda Carolina Pistolato
Hi @ANUTEJ,
Do you know how can I use this code you sent as an extension of a standard controller?