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
JosephJJosephJ 

Urgent ,column sorting not working

My arrow sorting is not working. Pressing my head from hours.Any issue with my query ? It is Urgent.

public class PagingTasksController1{

    public List<Task> tasks;
    public Integer CountTotalRecords{get;set;}
    public String QueryString {get;set;}
    public Integer OffsetSize = 0;
    private Integer QueryLimit = 10;
    public List<Task> lstTasks;
    public String searchText {get;set;}
 
    public Date mydate;
    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 PagingTasksController1 (){
        //CountTotalRecords= [select count() from Task];
         //String qStr2= '7/23/2014';
   
    }
    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 qStr2= searchText;
        String strnormal = '';
        try{
             mydate = date.parse(qStr2);
        }catch(Exception e)
        { }
          
        String strDate = '';
        if(mydate != null) {
         // strnormal = String.valueOf(mydate );
          String[] qstr3 = String.valueOf(mydate).split(' ',2);
          strDate = ' ActivityDate =  '+ qstr3[0] + ' ';
        }else{
      
           strDate  =  'Subject like \'%'+searchText +'%\' OR Status like \'%' +searchText+ '%\'';
        }
        String qStr = 'Select OwnerId,Subject,Status, ActivityDate from Task where '  + strDate   ;

         System.debug(qStr );
          
      
        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();
     }
   
  }
logontokartiklogontokartik
can you post the page code to see if its rendering correctly? Thank you
logontokartiklogontokartik
ok I copy pasted the exact code in my developer org and I dont see any issues and its sorting as expected. I tested it in firefox and chrome. Any errors you see in your debug logs??
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 . 

I think the pagination is also not working properly with the query changes ,any pointers ?