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
Mohita KalraMohita Kalra 

How can I set default sorted direction in lightning datatable?

I have a datatable which has column of type date. I want to set the default sorting order of the column. That means it should not allow the user to sort but date should be in order of latest date first. Is that possible ?
AbhishekAbhishek (Salesforce Developers) 
Hi,

Have you tried sortDirection attribute you can use this attribute to specify sorting direction Here is the sample code

updateColumnSorting(event) {
        var fieldName = event.detail.fieldName;
        var sortDirection = event.detail.sortDirection;
        // assign the latest attribute with the sorted column fieldName and sorted direction
        this.sortedBy = fieldName;
        this.sortedDirection = sortDirection;
        this.data = this.sortData(fieldName, sortDirection);
   }

For your reference, you can check this blog too,

https://developer.salesforce.com/forums/?id=9060G0000005rnlQAA

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks.
Mohita KalraMohita Kalra
Hi @Abhishek.. Thanks for the reply..Basically I dont want the user to do sorting. What I need is , I have a date column , the dates should be in ascending order. I dont want the end user to have sorting functionality. Will just default sort order work here ? Or I will have to use column sorting functions.
Alax JohnsAlax Johns
Our lights have the same qualities as those seen in Light my bricks. Apart from that, they are more affordable than those seen anywhere else. We are known to offer high-quality LEGO lighting bricks to fans of LEGOs. We understand that every set churned out by LEGO is top-notch, and that is why our lighting sets are of high quality too. https://gameofbricks.eu/collections/lego-light-kits