I have a visualforce page that displays some records from a custom object. The object has a date field and text field (that displays the date in a string), only the text is displayed in the page, but I'd like to sort by the date field. I added below the visualforce page.

<apex:page standardController="Monthly_Cloud_Bill__c" recordSetVar="unused" sidebar="false">

<apex:includeScript value="{!$Resource.UtilJS}" />
<apex:form >
<apex:pageBlock >
<apex:pageMessages />
<apex:pageBlock >
Note: All modifications made on the page will be lost if Return button is clicked without clicking the Save button first. 
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandButton value="Return" action="{!cancel}"/>
<apex:pageBlockTable value="{!selected}" var="a" id="table">
<apex:column headerValue="Account Name">
<apex:outputField value="{!a.account__r.name}"/>
<apex:column headerValue="Billing Month">
<apex:inputField value="{!a.Billing_Month_Text__c}"/>
<apex:column headerValue="Forecasted Number ">
<apex:inputField value="{!a.Forecasted_Number__c}"/>

  • 1
    How are you generating the selected property that is used by the pageblocktable? Commented Oct 6, 2013 at 10:48
  • Bob, I copied the VF page from the "mass update and mass edit" app
    – Itay B
    Commented Oct 7, 2013 at 6:08

1 Answer 1


Ok, I don´t see where you are declare the selected variable or action, so let´s say I have a custom controller that interacts with the account object and if I wanted to get the contacts linked to that account and sort them by date the method would be something like this:

public List<Contact> selected {
                results = [select id, name, from Contact where id = :accountId order by (here you specify the Date field) DESC or ASC];
                if (!results.isEmpty(){

            return selected;
  • Thanks Emilio, my VF is on a custom object related to that account (M-D relationship). I don't have any reference to the accountId in the VF page, how can I add that to the controller?
    – Itay B
    Commented Oct 7, 2013 at 6:32
  • That´s ok, if your model is a custom object which is the detail of the Account object that means you have access to any of the fields in the Account object, even if you don´t show them in your VF page.
    – Emilio
    Commented Oct 7, 2013 at 15:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .