+ Start a Discussion
JosephJJosephJ 

How to do arrow sorting on column

I've a requirement to do sorting . I did it with sort() but does only in ascending way. My client wants to have column sorting something like arrow sorting ,how can i attempt to do this with my code ? Please help. I'm really in a need to get this done.I'm referring to link : http://www.sundoginteractive.com/sunblog/posts/a-recipe-for-column-sorting-salesforce-visualforce-page which also doesnt work.

Stressed out with this.Thanks in advance,James.


    public class PagingTasksController{

    public List<Task> tasks;
    public Integer CountTotalRecords{get;set;}
    public String QueryString {get;set;}
    public Integer OffsetSize = 0;
    private Integer QueryLimit = 3;
    public List<Task> lstTasks;
    public String searchText {get;set;}
  
    public PagingTasksController (){
        //CountTotalRecords= [select count() from Task];
    }
  


    public List<Task> getTasks(){
        if(tasks == null){
            tasks = new List<Task>();
        }
        return tasks;
    }
  
    public void findTasks(){
        String qStr2 = 'Select count() from Task where Subject like \'%'+searchText+'%\' OR Status like \'%'+searchText+'%\'';
        CountTotalRecords = Database.countQuery(qStr2);
        queryTasks();
    }
  
    public void  queryTasks(){
        String qStr = 'Select OwnerId,Subject,Status,Priority from Task where Subject like \'%'+searchText+'%\' OR Status like \'%'+searchText+'%\' Order By Subject,Status limit ' + QueryLimit + ' offset ' + OffsetSize;
        tasks = Database.query(qStr);
        tasks.sort();

    }

    public Boolean getDisablePrevious(){
        if(OffsetSize>0){
            return false;
        }
        else return true;
    }

    public Boolean getDisableNext() {
        if (OffsetSize + QueryLimit < countTotalRecords){
            return false;
        }
        else return true;
    }

    public PageReference Next() {
        OffsetSize += QueryLimit;
        queryTasks();
        return null;
    }

    public PageReference Previous() {
        OffsetSize -= QueryLimit;
        queryTasks();
        return null;
    }

      public PageReference save() {
        update tasks;
        return ApexPages.CurrentPage();
     }
   
    }



     <apex:page controller="PagingTasksController">
    <apex:form >
        <apex:pageBlock title="Tasks" id="pgBlock">
           <apex:pageBlockButtons >
               <apex:commandButton action="{!save}" id="saveButton" value="Save"/>
               <apex:commandButton onclick="resetInlineEdit()" id="cancelButton" value="Cancel"/>
           </apex:pageBlockButtons>
   
        <apex:inlineEditSupport showOnEdit="saveButton, cancelButton"
                    hideOnEdit="editButton" event="ondblclick"
                    changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>
   
         <apex:inputText id="searchBox" value="{!searchText}"/>
        <apex:commandButton value="Search" reRender="pgTable,pgBlock" action="{!findTasks}"/>
          <apex:pageBlockTable value="{!Tasks}" var="tsk" id="pgTable" >
           <apex:column >
            <apex:outputLink value="{!URLFOR($Action.Task.Delete, tsk.id,['retURL'='/apex/Task_Assignment_Features'])}" >Delete</apex:outputLink>
</apex:column>
         
          <apex:column headerValue="Subject">
            <apex:outputField value="{!tsk.Subject}"/>
           </apex:column>
           <apex:column headerValue="Status">
             <apex:outputField value="{!tsk.Status}"/>
         </apex:column>
         <apex:column headerValue="Priority">
            <apex:outputField value="{!tsk.Priority}"/>
        </apex:column>
        <apex:column headerValue="OwnerId">
            <apex:outputField value="{!tsk.OwnerId}"/>
        </apex:column>
     </apex:pageBlockTable>
       <apex:pageBlockButtons >
                <apex:commandButton value="Previous" action="{!Previous}" rerender="pgTable,pgBlock"
                                    status="status" disabled="{!DisablePrevious}" />
                <apex:commandButton value="Next" action="{!Next}" reRender="pgTable,pgBlock"
                                    status="status" disabled="{!DisableNext}" />
                <apex:actionStatus id="status" startText="Please Wait..."/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
     <apex:enhancedlist type="Activity" height="800" rowsPerPage="50" customizable="False"/>
</apex:page>
Best Answer chosen by JosephJ
Jim JamJim Jam
You can use this as a guide ... I've amended your code to provide what you want on the subject field, so you just need to repeat that for the other columns you want to sort, and ensure the controller queries fire at the right time as well. I've highlighted the amendments below ..

public class PagingTasksController{

    public List<Task> tasks;
    public Integer CountTotalRecords{get;set;}
    public String QueryString {get;set;}
    public Integer OffsetSize = 0;
    private Integer QueryLimit = 3;
    public List<Task> lstTasks;
    public String searchText {get;set;}

    public string sortField = 'Subject';  // default sort column
    private string sApplySOQL = ''; 


// the current sort direction. defaults to ascending
    public String sortDir {
        get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;  }
        set;
    }

    // the current field to sort by. defaults to role name
    public String getsortField() {
        return sortField;
    }

    // the current field to sort by. 
    public void setsortField(string value) {
        sortField = value;
    }
             
    // toggles the sorting of query from asc<-->desc
    public void toggleSort() {
        // simply toggle the direction
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
       
        integer iIndex = sApplySOQL.indexOf('Order By');
        if (iIndex > -1){
          sApplySOQL = sApplySOQL.substringBefore('Order By');
          sApplySOQL = sApplySOQL + ' Order By ' + sortField + ' ' + sortDir;
        }
        tasks = Database.query(sApplySOQL );
    }

   
    public PagingTasksController (){
        //CountTotalRecords= [select count() from Task];
    }
 


    public List<Task> getTasks(){
        if(tasks == null){
            tasks = new List<Task>();
        }
        return tasks;
    }
 
    public void findTasks(){
        String qStr2 = 'Select count() from Task where Subject like \'%'+searchText+'%\' OR Status like \'%'+searchText+'%\'';
        CountTotalRecords = Database.countQuery(qStr2);
        queryTasks();
    }
 
    public void  queryTasks(){
        String qStr = 'Select OwnerId,Subject,Status,Priority from Task where Subject like \'%'+searchText+'%\' OR Status like \'%'+searchText+'%\' Order By Subject,Status limit ' + QueryLimit + ' offset ' + OffsetSize;
        sApplySOQL = qStr;
       
        tasks = Database.query(sApplySOQL );
        //tasks.sort();


    }

    public Boolean getDisablePrevious(){
        if(OffsetSize>0){
            return false;
        }
        else return true;
    }

    public Boolean getDisableNext() {
        if (OffsetSize + QueryLimit < countTotalRecords){
            return false;
        }
        else return true;
    }

    public PageReference Next() {
        OffsetSize += QueryLimit;
        queryTasks();
        return null;
    }

    public PageReference Previous() {
        OffsetSize -= QueryLimit;
        queryTasks();
        return null;
    }

      public PageReference save() {
        update tasks;
        return ApexPages.CurrentPage();
     }
  
    }



--------------


<apex:page controller="PagingTasksController">
    <apex:form >
        <apex:pageBlock title="Tasks" id="pgBlock">
           <apex:pageBlockButtons >
               <apex:commandButton action="{!save}" id="saveButton" value="Save"/>
               <apex:commandButton onclick="resetInlineEdit()" id="cancelButton" value="Cancel"/>
           </apex:pageBlockButtons>
  
        <apex:inlineEditSupport showOnEdit="saveButton, cancelButton"
                    hideOnEdit="editButton" event="ondblclick"
                    changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>
  
         <apex:inputText id="searchBox" value="{!searchText}"/>
        <apex:commandButton value="Search" reRender="pgTable,pgBlock" action="{!findTasks}"/>
          <apex:pageBlockTable value="{!Tasks}" var="tsk" id="pgTable" >
           <apex:column >
            <apex:outputLink value="{!URLFOR($Action.Task.Delete, tsk.id,['retURL'='/apex/Task_Assignment_Features'])}" >Delete</apex:outputLink>
</apex:column>
        
          <apex:column headerValue="Subject">

                <apex:facet name="header">
                    <apex:commandLink value="Subject" action="{!toggleSort}" rerender="pgTable" >
                    <apex:param name="sortField" value="Subject" assignTo="{!sortField}"/>
                    <apex:outputPanel rendered="{!BEGINS(sortField,'Subject')}">
                        &nbsp;<apex:image value="{!IF(sortDir = 'desc','/img/arrowDown.gif','/img/arrowUp.gif')}"/>
                    </apex:outputPanel>
                    </apex:commandLink>
                </apex:facet>   
  
               
                <apex:outputField value="{!tsk.Subject}"/>
           </apex:column>
           <apex:column headerValue="Status">
             <apex:outputField value="{!tsk.Status}"/>
         </apex:column>
         <apex:column headerValue="Priority">
            <apex:outputField value="{!tsk.Priority}"/>
        </apex:column>
        <apex:column headerValue="OwnerId">
            <apex:outputField value="{!tsk.OwnerId}"/>
        </apex:column>
     </apex:pageBlockTable>
       <apex:pageBlockButtons >
                <apex:commandButton value="Previous" action="{!Previous}" rerender="pgTable,pgBlock"
                                    status="status" disabled="{!DisablePrevious}" />
                <apex:commandButton value="Next" action="{!Next}" reRender="pgTable,pgBlock"
                                    status="status" disabled="{!DisableNext}" />
                <apex:actionStatus id="status" startText="Please Wait..."/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
     <apex:enhancedlist type="Activity" height="800" rowsPerPage="50" customizable="False"/>
</apex:page>

All Answers

Hargobind_SinghHargobind_Singh
Here is an article that might help you: 

https://developer.salesforce.com/forums?id=906F000000092mSIAQ 

Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
This is another article for sorting

http://salesforcesource.blogspot.in/2008/11/adding-sorting-capability-to.html
JosephJJosephJ
Thanks @hs1 and @Vidhyasagaran for the link . I'm stucked with the implementation part with my code.Not sure whether to implement Jquery or simply visualforce ? Pressing my head from hours and still no solution with the implementation.
Hargobind_SinghHargobind_Singh
James, I had implemented a list sort a few years ago, using simple visual-force. I was able to display the down and up arrow as well using character codes. I was passing parameters through javascript action methods to controller and then reloading the specific table area, and the controller method to get-data was sorting it from back-end, that also took care of pagination.

I'm afraid am pressed on time, else would've written some code, but I'll try to write something basic in the next few days if that helps. 

JosephJJosephJ
Thanks hs1 for the information. I'm still stucked and pressing my head thinking. I really need help for this one.Thanks in advance.
Jim JamJim Jam
You can use this as a guide ... I've amended your code to provide what you want on the subject field, so you just need to repeat that for the other columns you want to sort, and ensure the controller queries fire at the right time as well. I've highlighted the amendments below ..

public class PagingTasksController{

    public List<Task> tasks;
    public Integer CountTotalRecords{get;set;}
    public String QueryString {get;set;}
    public Integer OffsetSize = 0;
    private Integer QueryLimit = 3;
    public List<Task> lstTasks;
    public String searchText {get;set;}

    public string sortField = 'Subject';  // default sort column
    private string sApplySOQL = ''; 


// the current sort direction. defaults to ascending
    public String sortDir {
        get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;  }
        set;
    }

    // the current field to sort by. defaults to role name
    public String getsortField() {
        return sortField;
    }

    // the current field to sort by. 
    public void setsortField(string value) {
        sortField = value;
    }
             
    // toggles the sorting of query from asc<-->desc
    public void toggleSort() {
        // simply toggle the direction
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
       
        integer iIndex = sApplySOQL.indexOf('Order By');
        if (iIndex > -1){
          sApplySOQL = sApplySOQL.substringBefore('Order By');
          sApplySOQL = sApplySOQL + ' Order By ' + sortField + ' ' + sortDir;
        }
        tasks = Database.query(sApplySOQL );
    }

   
    public PagingTasksController (){
        //CountTotalRecords= [select count() from Task];
    }
 


    public List<Task> getTasks(){
        if(tasks == null){
            tasks = new List<Task>();
        }
        return tasks;
    }
 
    public void findTasks(){
        String qStr2 = 'Select count() from Task where Subject like \'%'+searchText+'%\' OR Status like \'%'+searchText+'%\'';
        CountTotalRecords = Database.countQuery(qStr2);
        queryTasks();
    }
 
    public void  queryTasks(){
        String qStr = 'Select OwnerId,Subject,Status,Priority from Task where Subject like \'%'+searchText+'%\' OR Status like \'%'+searchText+'%\' Order By Subject,Status limit ' + QueryLimit + ' offset ' + OffsetSize;
        sApplySOQL = qStr;
       
        tasks = Database.query(sApplySOQL );
        //tasks.sort();


    }

    public Boolean getDisablePrevious(){
        if(OffsetSize>0){
            return false;
        }
        else return true;
    }

    public Boolean getDisableNext() {
        if (OffsetSize + QueryLimit < countTotalRecords){
            return false;
        }
        else return true;
    }

    public PageReference Next() {
        OffsetSize += QueryLimit;
        queryTasks();
        return null;
    }

    public PageReference Previous() {
        OffsetSize -= QueryLimit;
        queryTasks();
        return null;
    }

      public PageReference save() {
        update tasks;
        return ApexPages.CurrentPage();
     }
  
    }



--------------


<apex:page controller="PagingTasksController">
    <apex:form >
        <apex:pageBlock title="Tasks" id="pgBlock">
           <apex:pageBlockButtons >
               <apex:commandButton action="{!save}" id="saveButton" value="Save"/>
               <apex:commandButton onclick="resetInlineEdit()" id="cancelButton" value="Cancel"/>
           </apex:pageBlockButtons>
  
        <apex:inlineEditSupport showOnEdit="saveButton, cancelButton"
                    hideOnEdit="editButton" event="ondblclick"
                    changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>
  
         <apex:inputText id="searchBox" value="{!searchText}"/>
        <apex:commandButton value="Search" reRender="pgTable,pgBlock" action="{!findTasks}"/>
          <apex:pageBlockTable value="{!Tasks}" var="tsk" id="pgTable" >
           <apex:column >
            <apex:outputLink value="{!URLFOR($Action.Task.Delete, tsk.id,['retURL'='/apex/Task_Assignment_Features'])}" >Delete</apex:outputLink>
</apex:column>
        
          <apex:column headerValue="Subject">

                <apex:facet name="header">
                    <apex:commandLink value="Subject" action="{!toggleSort}" rerender="pgTable" >
                    <apex:param name="sortField" value="Subject" assignTo="{!sortField}"/>
                    <apex:outputPanel rendered="{!BEGINS(sortField,'Subject')}">
                        &nbsp;<apex:image value="{!IF(sortDir = 'desc','/img/arrowDown.gif','/img/arrowUp.gif')}"/>
                    </apex:outputPanel>
                    </apex:commandLink>
                </apex:facet>   
  
               
                <apex:outputField value="{!tsk.Subject}"/>
           </apex:column>
           <apex:column headerValue="Status">
             <apex:outputField value="{!tsk.Status}"/>
         </apex:column>
         <apex:column headerValue="Priority">
            <apex:outputField value="{!tsk.Priority}"/>
        </apex:column>
        <apex:column headerValue="OwnerId">
            <apex:outputField value="{!tsk.OwnerId}"/>
        </apex:column>
     </apex:pageBlockTable>
       <apex:pageBlockButtons >
                <apex:commandButton value="Previous" action="{!Previous}" rerender="pgTable,pgBlock"
                                    status="status" disabled="{!DisablePrevious}" />
                <apex:commandButton value="Next" action="{!Next}" reRender="pgTable,pgBlock"
                                    status="status" disabled="{!DisableNext}" />
                <apex:actionStatus id="status" startText="Please Wait..."/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
     <apex:enhancedlist type="Activity" height="800" rowsPerPage="50" customizable="False"/>
</apex:page>
This was selected as the best answer
Hargobind_SinghHargobind_Singh
Joseph, here is a working example of a simple implementation: 

http://salesforce1blog.wordpress.com/2014/07/21/simple-sorted-table-by-column-headers/ 


JosephJJosephJ
Hi cmrc1001.3884211976265837E12, Thanks for sharing the information. Really helpful.

When i tried with "Status"  field ,it shows me the wrong of Sorting . Attaching picture for better understanding.What might be wrong in this area ? Thanks again. JamesStatus field displays niether in ASC nor DESC way.
Jim JamJim Jam
Need to see your code really.
JosephJJosephJ
I just added the above code for "Status" field in the Page

<apex:column headerValue="Status">
               <apex:facet name="header">
                    <apex:commandLink value="Status" action="{!toggleSort}" rerender="pgTable" >
                    <apex:param name="sortField" value="Status" assignTo="{!sortField}"/>
                    <apex:outputPanel rendered="{!BEGINS(sortField,'Status')}">
                        &nbsp;<apex:image value="{!IF(sortDir = 'desc','/img/arrowDown.gif','/img/arrowUp.gif')}"/>
                    </apex:outputPanel>
                    </apex:commandLink>
              </apex:facet>          
   
             <apex:outputField value="{!tsk.Status}"/>
         </apex:column>
Jim JamJim Jam
That looks OK, the issue is probably in the controller if you can post that code.
JosephJJosephJ
Here's my controller code.

public class PagingTasksController{

    public List<Task> tasks;
    public Integer CountTotalRecords{get;set;}
    public String QueryString {get;set;}
    public Integer OffsetSize = 0;
    private Integer QueryLimit = 3;
    public List<Task> lstTasks;
    public String searchText {get;set;}

    public string sortField = 'Subject';  // default sort column
    private string sApplySOQL = '';

// the current sort direction. defaults to ascending
    public String sortDir {
        get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;  }
        set;
    }

    // the current field to sort by. defaults to role name
    public String getsortField() {
        return sortField;
    }

    // the current field to sort by.
    public void setsortField(string value) {
        sortField = value;
    }
            
    // toggles the sorting of query from asc<-->desc
    public void toggleSort() {
        // simply toggle the direction
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
      
        integer iIndex = sApplySOQL.indexOf('Order By');
        if (iIndex > -1){
          sApplySOQL = sApplySOQL.substringBefore('Order By');
          sApplySOQL = sApplySOQL + ' Order By ' + sortField + ' ' + sortDir;
        }
        tasks = Database.query(sApplySOQL );
    }
  
    public PagingTasksController (){
        //CountTotalRecords= [select count() from Task];
    }

    public List<Task> getTasks(){
        if(tasks == null){
            tasks = new List<Task>();
        }
        return tasks;
    }

    public void findTasks(){
        String qStr2 = 'Select count() from Task where Subject like \'%'+searchText+'%\' OR Status like \'%'+searchText+'%\'';
        CountTotalRecords = Database.countQuery(qStr2);
        queryTasks();
    }

    public void  queryTasks(){
        String qStr = 'Select OwnerId,Subject,Status,Priority from Task where Subject like \'%'+searchText+'%\' OR Status like \'%'+searchText+'%\' Order By Subject,Status limit ' + QueryLimit + ' offset ' + OffsetSize;
        sApplySOQL = qStr;
      
        tasks = Database.query(sApplySOQL );
        //tasks.sort();

    }

    public Boolean getDisablePrevious(){
        if(OffsetSize>0){
            return false;
        }
        else return true;
    }

    public Boolean getDisableNext() {
        if (OffsetSize + QueryLimit < countTotalRecords){
            return false;
        }
        else return true;
    }

    public PageReference Next() {
        OffsetSize += QueryLimit;
        queryTasks();
        return null;
    }

    public PageReference Previous() {
        OffsetSize -= QueryLimit;
        queryTasks();
        return null;
    }

      public PageReference save() {
        update tasks;
        return ApexPages.CurrentPage();
     }
 }
Jim JamJim Jam
You need to change .. 

sApplySOQL = sApplySOQL + ' Order By ' + sortField + ' ' + sortDir;

.. to ... 

sApplySOQL = sApplySOQL + ' Order By ' + sortField + ' ' + sortDir +  ' limit ' + QueryLimit + ' offset ' + OffsetSize;

and ..

String qStr = 'Select OwnerId,Subject,Status,Priority from Task where Subject like \'%'+searchText+'%\' OR Status like \'%'+searchText+'%\' Order By Subject,Status limit ' + QueryLimit + ' offset ' + OffsetSize;

.. to ..

String qStr = 'Select OwnerId,Subject,Status,Priority from Task where Subject like \'%'+searchText+'%\' OR Status like \'%'+searchText+ '%\' Order By ' + sortField;


JosephJJosephJ
I tried with this, still the same case. What is happening is when i click on "Status" column , it neither sorts in Ascending order nor in Descending order.It is not working.:(
Jim JamJim Jam
Don't know what to say, works for me. Need to see your controller code. --- Original Message ---
Jim JamJim Jam
No need to see how your code looks now --- Original Message ---
JosephJJosephJ
Hey cmrc, are you referring to what was mentioned in my question ? didn't catch that.!
Jim JamJim Jam
There's no point posting the original code it's already there in your first post! Need to see how your code looks with all the changes you've made to see why it doesn't work. --- Original Message ---
JosephJJosephJ
Now  this my original controller code .

public class PagingTasksController{

    public List<Task> tasks;
    public Integer CountTotalRecords{get;set;}
    public String QueryString {get;set;}
    public Integer OffsetSize = 0;
    private Integer QueryLimit = 5;
    public List<Task> lstTasks;
    public String searchText {get;set;}

    public string sortField = 'Subject';  // default sort column
    private string sApplySOQL = '';

// the current sort direction. defaults to ascending
    public String sortDir {
        get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;  }
        set;
    }

    // the current field to sort by. defaults to role name
    public String getsortField() {
        return sortField;
    }

    // the current field to sort by.
    public void setsortField(string value) {
        sortField = value;
    }
            
    // toggles the sorting of query from asc<-->desc
    public void toggleSort() {
        // simply toggle the direction
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
      
        integer iIndex = sApplySOQL.indexOf('Order By');
        if (iIndex > -1){
          sApplySOQL = sApplySOQL.substringBefore('Order By');
          sApplySOQL = sApplySOQL + ' Order By ' + sortField + ' ' + sortDir +  ' limit ' + QueryLimit + ' offset ' + OffsetSize;
        }
        tasks = Database.query(sApplySOQL );
    }
  
    public PagingTasksController (){
        //CountTotalRecords= [select count() from Task];
    }



    public List<Task> getTasks(){
        if(tasks == null){
            tasks = new List<Task>();
        }
        return tasks;
    }

    public void findTasks(){
        String qStr2 = 'Select count() from Task where Subject like \'%'+searchText+'%\' OR Status like \'%'+searchText+'%\'';
        CountTotalRecords = Database.countQuery(qStr2);
        queryTasks();
    }

    public void  queryTasks(){
        String qStr = 'Select OwnerId,Subject,Status,Priority from Task where Subject like \'%'+searchText+'%\' OR Status like \'%'+searchText+ '%\' Order By ' + sortField;
        sApplySOQL = qStr;
      
        tasks = Database.query(sApplySOQL );
        //tasks.sort();

    }

    public Boolean getDisablePrevious(){
        if(OffsetSize>0){
            return false;
        }
        else return true;
    }

    public Boolean getDisableNext() {
        if (OffsetSize + QueryLimit < countTotalRecords){
            return false;
        }
        else return true;
    }

    public PageReference Next() {
        OffsetSize += QueryLimit;
        queryTasks();
        return null;
    }

    public PageReference Previous() {
        OffsetSize -= QueryLimit;
        queryTasks();
        return null;
    }

      public PageReference save() {
        update tasks;
        return ApexPages.CurrentPage();
     }
 
    }
Jim JamJim Jam
This code works for me. Maybe you're having an issue with your VF page. The status column should look like this ..

<apex:column headerValue="Status">
               <apex:facet name="header">
                    <apex:commandLink value="Status" action="{!toggleSort}" rerender="pgTable" >
                    <apex:param name="sortField" value="Status" assignTo="{!sortField}"/>
                    <apex:outputPanel rendered="{!BEGINS(sortField,'Status')}">
                        &nbsp;<apex:image value="{!IF(sortDir = 'desc','/img/arrowDown.gif','/img/arrowUp.gif')}"/>
                    </apex:outputPanel>
                    </apex:commandLink>
              </apex:facet>         
  
             <apex:outputField value="{!tsk.Status}"/>
         </apex:column>

You might need to do some browser debugging to see if there's some other issue.
JosephJJosephJ
I've noted one thing that if i keep only status like "Completed" an "In Progress" then it is working fine. While i keep more than 2 status like "Completed", "In Progress", "Deferred" "Waiting for someone else" then there it goes wrong.
Jim JamJim Jam
I think this might be something to do with the Task object itself. I think in certain scenarios (eg sorting by status) it always defaults the sort order by Due Date first. I'm not sure there is a workaround for this. Try debugging the SOQL that is being run and the data returned, and try and include the ActivityDate field to see if it is interfering with the query somehow.
JosephJJosephJ
There was no mistake indeed . What i found was , the sorting was correct as in the Task object , Status field , the sequencing is done in that way only and hence it fetches me the same result when i do sorting .Really thankful to you and appreciate the knowledge.
Jim JamJim Jam
Happy to help, mark the question as solved if you found the code useful as it might help others as well.