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
AtroxyAtroxy 

Permanent Sort for pageBlockTable

Heya,

 

I've got a VF pageBlockTable, where I can add my products to my opportunity. The VF shows the products in the order I added them to the pricebook. Is there anyway I can change that. Either sort by column X (alphabetically) or even better sort by colum X defining the order myself. E.g. Sort by Column X in Oder: Dog, Cat, Bird, Mouse.

 

I'm unsure where to look for a solution, does this go in the controller or my apex code?

 

Greetings!

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

In your Apex Code, you could add an ORDER BY clause to your SOQL, or you could sort() the list (default sorting), or you could even write a custom sort function (via a "wrapper" class and implementing the Comparable interface). It all depends on exactly what you're seeking to do, but should be possible.

All Answers

sfdcfoxsfdcfox

In your Apex Code, you could add an ORDER BY clause to your SOQL, or you could sort() the list (default sorting), or you could even write a custom sort function (via a "wrapper" class and implementing the Comparable interface). It all depends on exactly what you're seeking to do, but should be possible.

This was selected as the best answer
AtroxyAtroxy

I'll use ORDER BY for now, and created a new product field, which assigns a letter to ease sorting. In case anyone has an idea how to do the sorting and defining the order in the code, I'd be glad to hear it. ORDER BY only supports ASC/DSC.

sfdcfoxsfdcfox

This is a very crude example, but it demonstrates how such a feature might work:

 

<apex:page controller="customSort">
    <apex:form >
        <apex:pageBlock title="Sort Order">
            <apex:repeat value="{!sortIndexes}" var="sortItem">
                <apex:inputText value="{!sortOrder[sortItem]}"/>
            </apex:repeat>            
            <apex:pageBlockButtons >
                <apex:commandButton action="{!sort}" value="Sort"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:pageBlock title="Accounts">
            <apex:pageBlockTable value="{!accountList}" var="item">
                <apex:column >
                    <apex:outputText value="{!item.record.name}"/>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

public with sharing class customSort {
    public String[] sortOrder { get; set; }
    public AccountItem[] accountList { get; set; }
    
    public class AccountItem implements Comparable {
        customSort controller;
        public Account record { get; set; }
        
        public AccountItem(account a, customSort c) {
            record = a;
            controller = c;
        }
        public integer compareTo(Object x) {
            return String.join(controller.sortOrder,',').indexOf(record.name) - String.join(controller.sortOrder,',').indexOf(((AccountItem)x).record.name);
        }
    }
    
    public Integer[] getsortIndexes() {
        integer[] i = new integer[0];
        while(i.size() != accountList.size())
            i.add(i.size());
        return i;
    }
    
    public customSort() {
        sortOrder = new String[0];
        accountList = new AccountItem[0];
        for(Account a:[SELECT Id,Name FROM Account limit 10]) {
            sortOrder.add(a.name);
            accountList.add(new AccountItem(a,this));
        }
        sort();
    }
    
    public void sort() {
        accountList.sort();
    }
}

Of course, here's some things you could do to improve it:

 

1) Make the field you use dynamic (requires a ton of extra work, though...)

2) Improve the algorithm used to select a value (I'd recommend a map...)

3) Improve the UI so that users can simply click-and-drag values, or use buttons or something else.

4) Store the sorted order in a field on the record, so you can use ORDER BY for default ordering when the page loads.

 

... Tons of ways this could be improved, but I wanted to show that custom sorting can be done. This works by virtue of using the account's name as the sort key; when you juggle around the values in the text boxes and hit "sort", it will cause the page block below to show the new ordering.