datatable sorting sfdcmonkey

How To Implement Column Sorting In Salesforce Lightning DataTable

Lightning Component

Hi guys, Today in this post we are going to learn how we can implement custom column sorting in salesforce ‘lightning:datatable’ lightning base component.

custom sorting in lightning datatable

From developer console >> file >> new >> Apex Class 

Apex Controller : SortableAccountTableController.apxc

Check Also :  Powerful Lightning Datatable base component – Example Using Fieldset

From developer console >> file >> new >> Lightning Component 

Lightning Component : sortableAccountTable.cmp

JavaScript Controller :  sortableAccountTableController.js

JavaScript Helper :  sortableAccountTableHelper.js

  • check code comments 
Datatable Sorting Output :

custom sorting in lightning datatable


Related Resources :

Other popular Post :

Like our facebook page for new post updates. & Don’t forget to bookmark this site for your future reference.

if you have any suggestions or issue with it, you can post in comment box.

Happy Learning ?

About The contributor: 


3 Post Contribute

kiran b sfdcMonkey

I have 3.5 years of experience and working in lightning for the past 1 year. I am working as a developer in Deloitte.

Skills : Apex ,lightning , visualforce,sales cloud,JavaScript,Einstein analytics



Post By Kiran :

  1. How To Implement Infinity Or Lazy Loading In Salesforce Lightning DataTable
  2. How To Implement Column Sorting In Salesforce Lightning DataTable
  3. Develop a Simple To-Do List App With Salesforce Lightning Component


  • I am getting an error-

    Error in $A.getCallback() [Cannot read property ‘setParams’ of undefined] Callback failed: apex://SortableAccountTableController/ACTION$getAccounts Failing descriptor: {c:sortableAccountTable}

    • $A.get(“e.force:showToast”) event is handled by the container. It’s supported in Lightning Experience, Salesforce app, and Lightning communities only.
      if you are using this in standalone app or lightning out mode then add condition for it like : if(toastReference != undefined)

  • Hi Kiran,


    Could you please explain Me three things:

    In the function sortData in JS Helper:


    WHat does the lines  do?

    53>    var reverse = sortDirection == ‘asc’ ? 1: 1;

    60>    return reverse * ((a>b) (b>a));

    67>     return reverse * ((a>b) (b>a));


    Please kindly explain sir.





  • How to use parent field or lookup reation field of child custom object in client side controller. Ex: Parent : Course__c , Child : Class__c , field to use in client side controller : Course__r.category__c.

Leave a Reply