+ Start a Discussion
Vincent van Drunen LittelVincent van Drunen Littel 

​Sort a VisualForce Page Alphabetically


Hi, 

I have a visualforce page that provides me a list with accounts and their contacts. But it is a mess and all over the place, I need to get it to be sorted alphabetically.
And if possible a search box that would search within this page 

​Please let me know !  
Best Answer chosen by Vincent van Drunen Littel
Krishna SambarajuKrishna Sambaraju
If you are using controller to get the list of accounts and contacts you can order by Name in the SOQL Query as below.

List<Account> accounts = [select Id, Name from Account order by Name]; // for ascending order on Name
List<Account> accounts = [select Id, Name from Account order by Name DESC];// for descending order

Depending on the order required you can choose the query and use the corresponding list in the visualforce page. This will display the records in the order as required for you.

The same can be done with contacts.
 

All Answers

Krishna SambarajuKrishna Sambaraju
If you are using controller to get the list of accounts and contacts you can order by Name in the SOQL Query as below.

List<Account> accounts = [select Id, Name from Account order by Name]; // for ascending order on Name
List<Account> accounts = [select Id, Name from Account order by Name DESC];// for descending order

Depending on the order required you can choose the query and use the corresponding list in the visualforce page. This will display the records in the order as required for you.

The same can be done with contacts.
 
This was selected as the best answer
Vincent van Drunen LittelVincent van Drunen Littel
I've trying to find this answer all afternoon knowing it would be something simple .. and impossible to find on these forums.

Would you know how to create a SearchBox inside this VFpage aswell ?