+ Start a Discussion
ab84ab84 

Sorting results in visualforce

I have a visual force page that displays all ideas in a page block table.  I want to add a link to the side of the table that reads “Most discussed”.  When clicking on it, the ideas displayed will be sorted by the number of comments.  

Likewise, I want another link that says “popular” and when clicked it’ll sort by the number of votes.  

I’ve had a look at the below:

https://help.salesforce.com/apex/HTViewSolution?id=000171025&language=en_US

This kind of covers what I’m looking to do however I’m not sure how to implement the sort options.  My page uses a standard controller with an extension.

Can anyone suggest how I would code the sort options in the apex class and then implement in visual force?
Best Answer chosen by ab84
Mudasir WaniMudasir Wani
Hello,

The one easiest solution is that on the click of that link you need to get the result sorted DESC based on the your criteria field.
Say for example for most number of votes I suppose you are incrementing one field value so you can have a query like 

Let us consider that the field type for the votes field is Number  and the field name is votes__c and Object name is PropertyUnit__c
 
SELECT Name FROM PropertyUnit__c ORDER BY votes__c  DESC
Please have a look on the below link 
http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_select_orderby.htm

Let me know if you have any issue.

Don't forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.

 

All Answers

Mudasir WaniMudasir Wani
Hello,

The one easiest solution is that on the click of that link you need to get the result sorted DESC based on the your criteria field.
Say for example for most number of votes I suppose you are incrementing one field value so you can have a query like 

Let us consider that the field type for the votes field is Number  and the field name is votes__c and Object name is PropertyUnit__c
 
SELECT Name FROM PropertyUnit__c ORDER BY votes__c  DESC
Please have a look on the below link 
http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_select_orderby.htm

Let me know if you have any issue.

Don't forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.

 
This was selected as the best answer
Mudasir WaniMudasir Wani
Here is another nice link .
https://help.salesforce.com/apex/HTViewSolution?id=000171025&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000171025&language=en_US)

Remember you need to reRender the section with the updated data 
ab84ab84
Hi Mudasir, I'm still not sure how to implement that.

In the example you gave, is Select_Sorting_parameter__c a custom field on the object?  
Mudasir WaniMudasir Wani
Do you know the use of rendered and reRender 
If not refer to this link --http://gtr.net/visualforce-attributes-rendered-re-render-and-renderas/

Another thing you need is to sort the result for that paste your SOQL here 
What you need to do is to use orderBy filedName DESC
provided your field is integer type 
 
ab84ab84
The SOQL from my apex class is:
 
public List<Idea> getQPIdeas(){
        qpi = [select Id, Title, Body, Status, CommunityId, CreatorName, VoteTotal, CreatorSmallPhotoUrl,NumComments
              from Idea where CommunityId = '09aD00000007tkS'
              order by VoteTotal DESC LIMIT 25];  
              return qpi;
    }
I guess I need to have some kind of if statement so that if a selection is made the different SOQL is called.  How would I handle this if it was a hyperlink that showed the options of most discussed or popular rather than a drop down list?
 
Mudasir WaniMudasir Wani
I had a similar requirement where i need to sort the result based on Column as we can do in salesforce Standard list view 

I have achieved that using the below sample code 

In Apex Page
<apex:column title="Catalog" headerValue="Catalog" > 
	<apex:facet name="header">
		<apex:commandLink action="{!resultSortedByColumnName}" value="Catalog" status="waitStatus" id="catalogId" reRender="resultPanelId,debug,messagePanelId,myButtons" oncomplete="hideAllColumnAscDescImagesAndShowCurrentColums(this.id,{!ascendingSortingFlag},'IdPassed')">
			<apex:param name="orderByColumn" value="Product_Doc_Type__c" assignTo="{!orderByColumn}" />
			<apex:image url="{!$Resource.Sorted_Ascending}" width="20" height="20" rendered="{!ascendingSortingFlag}" id="catalogIdAscId" styleClass="imageAscDescClass"/>
			<apex:image url="{!$Resource.Sorted_Descending}" width="20" height="20" rendered="{!descendingSortingFlag}" id="catalogIdDescId" styleClass="imageAscDescClass"/>
		</apex:commandLink>
	</apex:facet>
	 <apex:outputField value="{!docs.Product_Doc_Type__c}" />
	
</apex:column>
Controller 
 
I have saved my SOQL in a variable 
searchSoql = 'Select name ,id from Object__c where '
public void executeSearchSOQL(String searchSoql, String orderByColumn) { 
	system.debug('this is the value of searchActionFlag --'+searchActionFlag);
	if(searchActionFlag){
		orderByColumn = 'Title';
	} 
	oldorderByColumnValue = orderByColumn;
	if(orderByColumn != ''){
		//searchSoql = searchSoql + ' order by '+orderByColumn;
		if(ascendingSortingFlag){
			 searchSoql+=  ' Order By '+orderByColumn+ ' ASC';
		  }else{
			searchSoql+=  ' Order By '+orderByColumn+' DESC';
		  }
	}
	searchSoql = searchSoql +' Limit ' + recordsPerPage;
	searchSoql+=' Offset '+ counter;
	searchActionFlag = false;
	this.searchSoql = searchSoql;
	system.debug('The value of searchSoql in executeSearchSOQL ------'+searchSoql);
	allDocuments = new  List <ContentVersion>();
	allDocuments = Database.query(searchSoql);
	system.debug('The documents retreived are  -----'+allDocuments);
	if(allDocuments != null && allDocuments.size()> 0){
		renderResultTable = true;
	}else{
		messageSectionFlag = true;
		renderResultTable = false;
		//ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,Label.noResultsFound));
	}
 }

Don't forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.
 
ab84ab84
I'm a lot closer.  I have it working in a fashion.

The field categories comes through as a multi-select...is there any way for me to make this appear as a single pick list (drop down menu) in visualforce instead?
ab84ab84
I figured it out.  :)

I used apex:selectOptions and apex:list to surface the items on the VF page and then a pagereference to refresh the page.