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
Virgilio GanataVirgilio Ganata 

Visualforce column sorting

Hi Team, Having troubles sorting my visualforce page by name, Im a bit playful with my demo org so please bear with me. 

Here is my VF 
<apex:page standardController="Lead" showHeader="false" sidebar="false" tabStyle="lead" >
<link href='https://fonts.googleapis.com/css?family=Ravie' rel='stylesheet' type='text/css' />
    
  <style>
      .alef1 {
        font-family: 'Alef', serif;
        font-size: 24px;
      }
      
      .alef2 {
        font-family: 'Alef', serif;
        font-size: 14px;
      }
         
   
       .activeTab {background-color: #1798c1 ; font-family: 'Alef';font-style: Italic; color:white; width: 100px; height: 23px; padding-top:2px; font-size: 15px; font-style:'Alef'; border:0px;
         background-image:none; border-radius: 10px;}

      .inactiveTab {background-color: #1798c1 ; font-family: 'Alef'; font-style: Italic; color:white; width: 100px; height: 15px; padding-top:2px; font-size: 15px; font-style:bold; border:0px;
         background-image:none; border-radius: 10px;}
    </style>
    
    <div class="alef1"><i>Attach your favorite Music using the attach file button</i></div><br/>
    <div class="alef2"><i><marquee>***Note: Only .mp3 format are supported</marquee></i></div><br/>
<apex:tabPanel switchType="client" selectedTab=" tabdetails" id="LeadTabPanel" tabClass="activeTab" inactiveTabClass="inactiveTab" contentStyle="font-family: Poiret One;">  
 
<apex:tab label="Play Here" name="LeadDetails">
<apex:page standardController="Lead">
</apex:page>
<apex:page standardController="Lead" id="pg" extensions="PlayAudio" sidebar="true">
<apex:includeScript value="{!URLFOR($Resource.AudioPlayer, 'audio-player/audio-player.js')}"/>
<script type="text/javascript">  
     AudioPlayer.setup("{!URLFOR($Resource.AudioPlayer, 'audio-player/player.swf')}", {  
         width: 400  
     });  
</script> 

<script type="text/javascript">  
    function playAudio(LeadObj)
        {
            AudioPlayer.embed("audioplayer_1", {soundFile: LeadObj});
        }  
</script>
<apex:form id="frm">
    <apex:pageBlock id="pb">
        <apex:variable value="{!0}" var="count" />
        <apex:pageBlockTable value="{!listAttachment}" var="item" id="pbt">
            <apex:column headerValue="Title">
            
                {!item.Name}
            </apex:column>
            
            <apex:column id="colPlayer" rendered="{!CONTAINS(item.ContentType, 'audio')}">
                <p id="{!count}.audioplayer"></p>
                <script type="text/javascript">
                    AudioPlayer.embed("{!count}.audioplayer", {soundFile: 'https://trialdemo.my.salesforce.com/servlet/servlet.FileDownload?file={!item.Id}'});
                </script>
                <apex:variable value="{!count+1}" var="count" />
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>
</apex:page>
</apex:tab>
<apex:tab label="Attach Here" name="NotesAndAttachments" id="tabNoteAtt">

         <apex:relatedList subject="{!Lead}" list="CombinedAttachments" />

      </apex:tab>
</apex:tabPanel>
</apex:page>


and here is my class

//extensions to take care all the lead and activity related attachments and 
public without sharing class PlayAudio {
    
    public List<Attachment> listAttachment {get;set;}
    public Lead lead {get;set;}
    
    //constructor
    public PlayAudio(ApexPages.StandardController controller) {
        
        //get the controller instance
        lead = (Lead)controller.getRecord();
        
        //initialize the attachments list
        listAttachment = new List<Attachment>();
        
        //get all the lead related attachments
        listAttachment = [Select Id, ContentType, Name from Attachment where ParentId = :lead.Id];
        
        //get all the related tasks attachments
        List<Task> listTask = [Select WhatId from Task where WhoId =: lead.Id ];
        listAttachment.addAll([Select Id, ContentType, Name from Attachment where ParentId IN :listTask]);
        
        //get all the related events attachments
        List<Event> listEvent = [Select WhatId from Event where WhoId =: lead.Id ];
        listAttachment.addAll([Select Id, ContentType, Name from Attachment where ParentId IN :listEvent]);
        
    }
    
    @isTest
    private static void testPlayAudio()
        {
            //create a lead record
            Lead lead = new Lead(LastName='test', Company='test');
            insert lead;
            
            //start test from here 
            Test.startTest();
            
            PlayAudio controller = new PlayAudio(new ApexPages.StandardController(lead));
            
            System.assert(lead != null);
            //stop test here
            Test.stopTest();
        }

}

So this gets the attachments from Leads attachments related list to play audio mostly used with voicemails. Id really like to sort by name
Best Answer chosen by Virgilio Ganata
Temoc MunozTemoc Munoz
Hi Virgilio,

Like Shaijan mentioned; can you just try this in your code:
listAttachment = [Select Id, ContentType, Name from Attachment where ParentId = :lead.Id order by Name ASC];
?

Thanks
 

All Answers

Shaijan ThomasShaijan Thomas
Not sure what you are trying to sort. If its a record sord. add sort in query itself
Thanks
Virgilio GanataVirgilio Ganata
Hi, So yes, I'd like to sort the record by name so it will be alphabetical. currently its showing in random.
Temoc MunozTemoc Munoz
Hi Virgilio,

Like Shaijan mentioned; can you just try this in your code:
listAttachment = [Select Id, ContentType, Name from Attachment where ParentId = :lead.Id order by Name ASC];
?

Thanks
 
This was selected as the best answer
Virgilio GanataVirgilio Ganata
@Temoc, Just did and it worked :) Thanks a lot
Temoc MunozTemoc Munoz
Awesome. Thanks Virgilio!