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 

sorting on column

Hi,

I need to implement Sorting in my Visualforce Page on column. I've used my Custom Controller Class to Display records in apex : pageBlockTable, but I'm not able to implement sorting there.Please help. 

Thanks in advance
James

<apex:page standardController="Task" extensions="TaskSearchController">
       <apex:form id="searchForm">
      <apex:PageBlock mode="inlineEdit">      
      <apex:pageblockSection id="searchBlockSection">
       <apex:pageBlockSectionItem id="searchBlockSectionItem">
        <apex:outputLabel >Keyword</apex:outputLabel>
            <apex:panelGroup >
                <apex:inputtext id="searchTextBox" value="{!searchText}">  </apex:inputtext>
                <apex:commandButton Id="btnSearch" action="{!Search}" rerender="renderBlock" status="status" title="Search" value="Search"> </apex:commandButton>
            </apex:panelGroup>
        </apex:pageBlockSectionItem>
    </apex:pageblockSection>
    <apex:actionStatus id="status" startText="Searching... please wait..."/>      
    <apex:pageBlocksection id="renderBlock" >
        <apex:pageblocktable value="{!SearchResults}" var="o" rendered="{!NOT(ISNULL(SearchResults))}">
            <apex:outputLink value="/{!o.Id}">{!o.Subject}</apex:outputLink>
            <apex:column value="{!o.Subject}"/>
            <apex:outputLink value="/{!o.Id}">{!o.Status}</apex:outputLink>
            <apex:column value="{!o.Status}"/>
        </apex:pageblocktable>     
     </apex:pageBlocksection>
      </apex:pageblock>
     </apex:form>
     <apex:enhancedlist type="Activity" height="800" rowsPerPage="50" customizable="False"/>
    </apex:page>


   // Apex 

     public class TaskSearchController
    {
        public apexpages.standardController controller{get;set;}
       
        public Task l;
        public List<Task> searchResults {get; set; }
        public string searchText
    {
    get
        {
            if (searchText==null) searchText = '';
            return searchText;
        }
    set;
        }

    public TaskSearchController(ApexPages.StandardController controller)
    {
        this.controller = controller;
        this.l = (Task) controller.getRecord();
    }
    public PageReference search()
    {
        if(SearchResults == null)
        {
            SearchResults = new List<Task>();
        }
        else
        {
            SearchResults.Clear();
        }

   String qry = 'Select Id,Subject,Status,ActivityDate from Task where Subject like \'%'+searchText+'%\' OR Status like \'%'+searchText+'%\' Order By Subject,Status,ActivityDate';
//    System.debug(qry);
    SearchResults = Database.query(qry);
   // System.debug(SearchResults);
    return null;
  }
}


Best Answer chosen by JosephJ
RishavRishav
Hi james
                       try this:                             SearchResults = Database.query(qry);
                                                               searchResults.sort();    // just add this line you may get your desired result.
   
Thanks
Rishav

All Answers

JosephJJosephJ
   For Subject and Status column sorting needs to be implemented.
RishavRishav
Hi james
                       try this:                             SearchResults = Database.query(qry);
                                                               searchResults.sort();    // just add this line you may get your desired result.
   
Thanks
Rishav
This was selected as the best answer
JosephJJosephJ
Thanks Rishav. It worked. But what if the requirement comes that arrow should be there on column name and clicking on it should sort it ? How will this go realted with my code ?
RishavRishav
HI james,
                   do you want to sort a particular column?
Vasani ParthVasani Parth
Yes, what if i want to sort Subject and Status column ?
JosephJJosephJ
@Rishav , I want to sort it on 'Subject' and 'Status' column then ? It should work for both ASC and DESC.Something like arrow sorting i want , how do i implement it .Thanks in adavnce.