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

Sort PageBlockTable

I'm a total newbie to writing controllers, but I would like to learn.


How do I sort a PageBlockTable?  I have read several posts on here and understand I need to use the ORDER BY statement but I'm lost how to implement it in a controller.


Please Help!


Do you want it sorted by default or should it become sorted when the user clicks a column?

By Default

Can you post your code? At least wherever you build the list of objects used in your page block table.


You should be using a list which will populate data in your page block table. This list is populated from the query running in your controller. Just add an order by 'field name' clause at the end of the query, and you will be good.



Please mark it as Solved if this helps you, so that others can benefit!




I don't have a controller associated with this table. It is simply a VF page with a related list.

I just realized when I replied SURE from my email, the code didn't come through.  Here is it....sorry about that:


<apex:page sidebar="false" showHeader="false" cache="false" standardController="Apartment__c" contentType="application/" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!Apartment__c.Units__r}" var="item">
<apex:column value="{!item.ID}" />
<apex:column value="{!item.Unit_Number__c}" />
<apex:column value="{!item.Nickname__c}" />
<apex:column value="{!item.Available__c}" />
<apex:column value="{!item.Bedrooms__c}" />
<apex:column value="{!item.Bathrooms__c}" />
<apex:column value="{!item.Square_Feet__c}" />
<apex:column value="{!item.Layout__c}" />
<apex:column value="{!item.Rent__c}" />
<apex:column value="{!item.Deposit__c}" />
<apex:column value="{!item.Washer_Dryer__c}" />
<apex:column value="{!item.Attached_Garage__c}" />
<apex:column value="{!item.Wheelchair_Accessible__c}" />