0

I am creating a table of values that need to be displayed in a specific order. My values are pulled from a map in an apex:repeat and I just discovered that apex:repeat defines the order for you.

My two options are to find a new way to use apex:pageBlockTable / apex:dataTable OR to figure out how to pull the key out, save it into a list, and then some how re-associate the values to the keys.

I have seen a few posts about similar issues with re-defining the order of the map but none of the answers seem to have a solid solution.

I have tried messing around with a pageBlockTable inside of a pageBlockTable, apexRepeat inside of the table, .sort() method on the keys of the map, dataTable, dataList. I feel the easy way to do this without having to change the backend logic would be to find an alternative to apex:repeat.

See below code:

VF

<apex:repeat value="{!caseCommMap}" var="caseNum">
    <th colspan="5" id="casenumber">{!caseNum}</th>
        <apex:repeat value="{!caseCommMap[caseNum]}" var="num">
            <tr>
                <td>{!num.CreatedDate}</td>
                <td>{!num.CommentBody}</td>
                <td>{!num.CreatedBy.Name}</td>
            </tr>
        </apex:repeat>
    </apex:repeat>

APEX

caseCommMap = new Map<String, List<CaseComment>>();
for ( Case c : [SELECT (SELECT Id, CommentBody, CreatedDate, ParentId, CreatedById, CreatedBy.Name FROM CaseComments), AccountId, CaseNumber FROM Case WHERE AccountId =: currentAcctPageId ORDER BY CaseNumber, CreatedDate DESC]) {
    for (CaseComment caseComm : c.CaseComments) {
        if(caseCommMap.containsKey(c.CaseNumber)){
            caseCommMap.get(c.CaseNumber).add(caseComm);
        }
        else {
            caseCommMap.put(c.CaseNumber, new CaseComment[]{
                caseComm
            });
        }
    }
}
7
  • because apex:repeat overrides any order you determine. So when I system debug that map, the order is correct. but when I push it to apex:repeat, it is re-ordered in ASC order. – Olivia Apr 25 '17 at 17:19
  • What's wrong with a simple sub-query? – sfdcfox Apr 25 '17 at 17:20
  • could you elaborate on that @sfdcfox? – Olivia Apr 25 '17 at 17:23
  • as far as I know, no matter what I do with my queries, the apex:repeat will over ride the order – Olivia Apr 25 '17 at 17:23
  • 3
    No, it's just because you're using Maps, which have an indeterminate order. If you used a simple List<Case> it'd be in the order you expect. – sfdcfox Apr 25 '17 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.