+ Start a Discussion
di_zoudi_zou 

How do I sort cases using a pageBlockTable

I have a Visualforcepage with an apex:pageBlocKTable displaying Cases. I am trying to add the ability to sort the cases by CaseNumber.

I looked over this wiki page: http://wiki.developerforce.com/page/Sorting_Tables and implemented it, but I am getting an error.

 

This is my Visualforce page:

 

 

<apex:page standardController="Case" extensions="caseSort" recordSetVar="Case" sidebar="true" showHeader="true">
    <apex:form >
        <apex:pageBlock title="Cases">
                <apex:pageBlockTable value="{!case}" var="c" rows="50" id="cases_table" >
                    <apex:column >
                        <a target="_parent" href="{!URLFOR($Action.Case.View, c.id)}">{!c.CaseNumber}</a>
                        <apex:facet name="header">
                            <apex:commandLink value="{!$ObjectType.Case.Fields.CaseNumber.Label}" action="{!doSort}" rerender="table">
                                <apex:param name="sortField" value="CaseNumber" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                    </apex:column>
                    <apex:column value="{!c.ContactId}" />
                    <apex:column >
                        <a target="_parent" href="{!URLFOR($Action.Case.View, c.id)}">{!c.Subject}</a>
                        <apex:facet name="header">Subject</apex:facet>
                    </apex:column>
                    <apex:column value="{!c.Status}" />
                    <apex:column value="{!c.Priority}" />
                    <apex:column value="{!c.CreatedDate}" />
                </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
    <base target="_parent"/>
</apex:page>

 

and this is my controller:

 

public class caseSort {

    public caseSort(ApexPages.StandardSetController controller) {

    }

    List<Case> cases;
    public String sortField {get; set;}
    public String previousSortField {get; set;}

    public List<Case> getCases() {
        if(cases == null){
            cases = [select CaseNumber, ContactId, Subject, Status, Priority, CreatedDate from Case];
        }
        return cases;
    }
            
    public void doSort(){
        String order = 'asc';
        
        /*This checks to see if the same header was click two times in a row, if so 
        it switches the order.*/
        if(previousSortField == sortField){
            order = 'desc';
            previousSortField = null;
        }else{
            previousSortField = sortField;
        }
       
        //To sort the table we simply need to use this one line, nice!
        superSort.sortList(cases,sortField,order);
    }
}

 

When I go to the page and click the CaseNumber header to sort, I get this error:

 

    Attempt to de-reference a null object

    Error is in expression '{!doSort}' in component <apex:page> in page casestestpage
 

    An unexpected error has occurred. Your development organization has been notified.

 

What am I doing wrong, and what do I have to do to fix it? Thanks!