+ Start a Discussion
Vincent Drader 2Vincent Drader 2 

Sorting a visualforce table (pdf output)

Nonprofit admin moonlighting as a developer here. I have a visualforce page that has a table I would like to sort by one of the columns A to Z. The visualforce page is just a pdf print out of certain things from the object and related lists. See below for the snippet of code I have for my table. How can I sort the table A-Z by "Latin Tree Name" column, which takes the {!tr.Tree_Name__r.Name} values?

<table width="100%" border="1">
<th width="15%">CRM Record #</th><th width="35%">Latin Tree Name</th><th width="15%">Stock Type</th><th width="17%">Form</th><th width="10%">Size</th><th width="8%">Quantity</th>
<apex:repeat value="{!Campaign.Tree_Records__r}" var="tr">
<tr>
<td><apex:outputtext value="{!tr.Name}" /></td>
<td><apex:outputtext value="{!tr.Tree_Name__r.Name}" /></td>
<td>{!tr.Stock_Type__c}</td>
<td>{!tr.Tree_Form__c}</td>
<td>{!tr.Caliper_Size__c}</td>
<td>{!ROUND(tr.Quantity_Requested__c,0)}</td>
</tr>
</apex:repeat>
</table>
3 Creeks3 Creeks
Just use "ORDER BY Tree_Name__r.Name" in the query and then it will come into the table sorted.
Vincent Drader 2Vincent Drader 2
Tried a few things. Couldn't get that to work. Exactly where would your recommend inserting the order by? And what would be the exact syntax?
3 Creeks3 Creeks
Presumably you have an Apex controller that is doing a query of the data that is then displayed by the VF page that you are building.  If so, that query can use the ORDER BY keywords that will tell the DB to sort the data by the field that you specify before it is returned to the controller.   See the docs on it here: https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_orderby.htm

You can also use the Query Editor tab of the Developer Console (Set up -> Developer Console) to test out the query before adding it to your query.