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
VarunCVarunC 

Sorting Column Headers of Grid

Hi,

I need to implement Sorting in my Visualforce Page. I've used my Custom Controller Class to Display records in apex : pageBlockTable, but I was not able to implement sorting in the list.

Can anyone help me out here?

- Varun
Rajesh ShahRajesh Shah

I had implemented the sorting functionality in S-Control. However the same thing can be implemented in Visualforce page. Basically when a User clicks on Header of any column, we will need to call a Controller function.

The controller function will take the name of Column as input. It then needs to sort the list of your data as per the column. Heres how you can do that.

1. Create a List of all records.

2. Create a Map which will store as Key the unique values of the Column you are sorting and Value will be a list of Records which has those values.

3. When Creating a Map, also create a List which will store the unique values of Column you are sorting.

4. Sort the List. ( Apex list has an inbuilt function of Sort. Use that)

5. Using the map, recreate the List of Records in sorted order.

6. Use this records in the page block table.

I am attaching the code of the function that I wrote so that you can understand the steps better

/********************************************

// This function is used for sorting of columns on search result screen

webservice static Saved_Listings__c[] sortLists(Integer x, Saved_Listings__c[] savedLists)

{

try

{

Map<Double,List<Saved_Listings__c>> savedListingsDblMap = new Map<Double,List<Saved_Listings__c>>();

Map<String,List<Saved_Listings__c>> savedListingsStrMap = new Map<String,List<Saved_Listings__c>>();

List<String> strFieldValues = new List<String>();

List<Double> dblFieldValues = new List<Double>();

for(Integer i=0; i<savedLists.size(); i++)

{

List<Saved_Listings__c> temp = new List<Saved_Listings__c>();

// X is used for identifying the column that is to be sorted.

if(x==0)

{

if(savedListingsDblMap.containsKey(savedLists[i].MLS_ID__c))

temp = savedListingsDblMap.get(savedLists[i].MLS_ID__c);

else

dblFieldValues.add(savedLists[i].MLS_ID__c);

temp.add(savedLists[i]);

savedListingsDblMap.put(savedLists[i].MLS_ID__c,temp);

}

if(x==1)

{

if(savedListingsStrMap.containsKey(savedLists[i].Address__c))

temp = savedListingsStrMap.get(savedLists[i].Address__c);

else

strFieldValues.add(savedLists[i].Address__c);

temp.add(savedLists[i]);

savedListingsStrMap.put(savedLists[i].Address__c,temp);

}

if(x==2)

{

if(savedListingsDblMap.containsKey(savedLists[i].Total_Full_Baths__c))

temp = savedListingsDblMap.get(savedLists[i].Total_Full_Baths__c);

else

dblFieldValues.add(savedLists[i].Total_Full_Baths__c);

temp.add(savedLists[i]);

savedListingsDblMap.put(savedLists[i].Total_Full_Baths__c,temp);

}

if(x==3)

{

if(savedListingsDblMap.containsKey(savedLists[i].Total_Bedrooms__c))

temp = savedListingsDblMap.get(savedLists[i].Total_Bedrooms__c);

else

dblFieldValues.add(savedLists[i].Total_Bedrooms__c);

temp.add(savedLists[i]);

savedListingsDblMap.put(savedLists[i].Total_Bedrooms__c,temp);

}

}

List<Saved_Listings__c> sortedSavedLists = new List<Saved_Listings__c>();

if(x==6 || x==0 || x==2 || x==3)

{

dblFieldValues.sort();

for(Integer i=0; i<dblFieldValues.size(); i++)

{

sortedSavedLists.addAll(savedListingsDblMap.get(dblFieldValues[i]));

}

}

else

{

strFieldValues.sort();

for(Integer i=0; i<strFieldValues.size(); i++)

{

sortedSavedLists.addAll(savedListingsStrMap.get(strFieldValues[i]));

}

}

return sortedSavedLists;

}

catch(Exception e)

{

throw e;

}

}

*************************************/

Another addition you can add is to have a boolean variable to decide whether the sorting would be descendin order or Ascending order. I had included that part in the s-control.

Let me know in case of more information.

 

 

VarunCVarunC
Wow. will check it.

Thanks.
TehNrdTehNrd
Here is a great blog post on sorting. The code is very efficient.

http://blog.sforce.com/sforce/2008/09/sorting-collect.html


Sam.arjSam.arj
Also this is another way to implement this feature:

http://salesforcesource.blogspot.com/2008/11/adding-sorting-capability-to.html

http://salesforcesource.blogspot.com/2008/11/adding-sorting-capability-to.html

VarunCVarunC
Thanks. Will check it out.
TehNrdTehNrd
It's worth noting that if your table as any input fields the values will not be lost with this method:

http://blog.sforce.com/sforce/2008/09/sorting-collect.html

With the requery method all input values would be lost.
VarunCVarunC
Thanks. But i got it working well in my case from Sam's suggestion.

Though thanks for all your efforts and suggestions. :)
Rajesh ShahRajesh Shah

Check out the following link:

http://wiki.developerforce.com/index.php/Sorting_Tables

 

The code presented is dynamic which is really good.

 

AlanLamAlanLam

@

 

Hi,

 

the link you provided seems to be down.

 

Do you have updated link for this?

 

Sounds great..

AlanLamAlanLam
& Retaining the input values is required