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
PrazziePrazzie 

Sorting records using StandardSetController

Hi,

 

I am using the following code. Yet the order of the records is not fetched according to the query. When I run the query independently it is providing right results. How to get rid of the problems?

 

class

 

public List<Utility_Object__c> getUnIdenfiedEntries() {
        
        return (List<Utility_Object__c>) setCon.getRecords();       
        
    }
    
     public ApexPages.StandardSetController setCon {
        get {
        
            if (setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                [Select salesAmount__c,Reported_Buyer__c From Utility_Object__c where Related_Account__c = :accountId AND Report_Level__c=:DISTI_VAL
                Order by salesAmount__c desc limit 100]));
                setCon.setPageSize(this.pageSize);
                this.recordCount = setCon.getResultSize();
                
            }
            return setCon;
        }
        set;
            
    }

 

 

page

 

<apex:pageBlockTable value="{!unIdenfiedEntries}" var="entry" id="entryList">
                <apex:column headerValue="Reported Buyer" value="{!entry.Reported_Buyer__c}"/>
                <apex:column headerValue="Sales" value="${!entry.salesAmount__c}"/>
            </apex:pageBlockTable>

 

Help will be appreciated

 

*werewolf**werewolf*

What order is the set controller picking up then if not that of salesAmount__c?

PrazziePrazzie

look like a random one...it is taking the amount which close to the highest sales amount but not the highest one actually

*werewolf**werewolf*

Well that's probably because of the LIMIT 1000.  It will pick the first 1000 records and sort them, so if the highest record is outside that 1000, well, it won't get included.  When you put a limit in there, the limit is applied *before* the sort, not after it.

PrazziePrazzie

That is unlikely because when I am using the same query without StandardSetController the sorting order seems to  be right

*werewolf**werewolf*

A fair point.  What happens if you create the StandardSetController from a List<Utility_Object__c> instead of from the query locator?  It shouldn't really make a difference but you never know.

PrazziePrazzie

Nope..it's also not working :(