[SalesForce] Sorting column in Data table

Is there any native Apex solution to get option to sort a column in apex data table?

Best Answer

If you are using apex:dataTable then you can pre-sort the data in your SOQL query. See here for an example of using apex:dataTable: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_dataTable.htm

Using the query on that page as an example, you could sort it as follows:

public List<Account> getAccounts() {
    if(accounts == null)
        accounts = [SELECT name, owner.name FROM account ORDER BY name DESC LIMIT 10];
    return accounts;
}

Notice I added the ORDER BY name DESC which will then return the results of Accounts by Name in descending order.

Related Topic