+ Start a Discussion
umair ayazumair ayaz 

A function to reorder the table

Hi saleforce experts I am stucj with a small problem where i want to reorder my table according to order number so right now i have created a order number field and have ordered the table according to order number and the issue i am facing right now is for example if i change the order number 20 to order number 5 the record in order number 5 should change to order number 6 which means all the records between order number 5 to order number 20 should be added by 1. I have added my visualforce page codes and my controller codes below. please guide me on how it could be done. Thanks in advance

My visualforce code
<apex:page controller="OrderAccountController" >
    <apex:pageBlock title="List of accounts">
        <apex:pageBlockTable value="{!account}" var="a">
            <apex:column value="{!a.Name}"/>
            <apex:column value="{!a.Industry}"/>
            <apex:column value="{!a.Type}"/>
            <apex:column headerValue="Order Number">
            <apex:inputField value="{!a.order__c}"/>
        <apex:commandButton value="Reorder" action="{!save}" />

My apex code
public class OrderAccountController {
    public List <Account> account=new List <Account>();
    public Account act{get;set;}
     public List <Account> getAccount() {
        account = [SELECT Id, Name, Site, Type, accountNumber, CustomerPriority__c, order__c, industry FROM Account order by order__c ];
        return account;
    public PageReference save() {
        try {
            update account;

        catch(Exception e){
            System.debug('Exception occurred '+String.valueOf(e));
        return NULL;


Rahul KumarRahul Kumar (Salesforce Developers) 
Glyn Anderson 3Glyn Anderson 3
This is a difficult problem.  I can't provide the code, but I can describe the process.  Create a wrapper class that contains the Account records and a separate Order value initialized to the Account's current value.  The wrapper class should implement the Comparable interface.  Have your PageBlockTable iterate over a list of wrappers.  On save, sort all the records by their new Order values, preferring records that have changed (they go before records that haven't changed, but have the same value).  Your 'compareTo' method should handle this case.  Then, iterate the sorted list of wrappers and assign Order values to the Account records in sequential order.  Then you can update the records.
Glyn Anderson 3Glyn Anderson 3
Did any of these answers solve your problem?  If so, please mark the question as "Solved".  If not, let us know.  If you solved it yourself another way, please post your solution.  Thanks!