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
John NeffJohn Neff 

How do I set items in an array to appear in alphabetical order?

Hello, 

Using this article (http://blog.jeffdouglas.com/2011/03/02/dynamically-group-display-query-results/) I have created a VF page and controller that dynamically groups custom object records by their associated campaign. 

It works great, except the campaigns in the array seem to display in random order on the VF page - and my users don't like that!

Is there a way that I can tweak my controller (shown below) to make the array of campaigns appear in alphabetical order? 

Here is my controller: 
 
public with sharing class AllSpotController{

public List <Job_Number__c> SPOT {get;set;}
public String[] campaigns {get;set;}

public void load() {

  SPOT = [Select id, name, Thumbnail_URL__c, ISCI__c, Title__c, Project__c, Campaign__r.Name, Nets_Running__c, View_Link__c, Internal_Title__c, Legal__c from Job_Number__c where ISCI__c <> null];
  
  Set<String> campaignSet = new Set<String>();
  for (Job_Number__c j : SPOT)
      campaignSet.add(j.Campaign__r.Name);
      
      campaigns = new String[campaignSet.size()];
      Integer i = 0;
      for (String campaign : campaignSet) {
      campaigns[i] = campaign;
      i++;
        }
    }
}

Thanks in advance!

John
Best Answer chosen by John Neff
Ankit SehgalAnkit Sehgal
public with sharing class AllSpotController
{

public List <Job_Number__c> SPOT {get;set;}
public list<String> campaigns {get;set;}

public void load() 
{
  campaigns= new List<String>();
  SPOT = [Select id, name, Thumbnail_URL__c, ISCI__c, Title__c, Project__c,       Campaign__r.Name, Nets_Running__c, View_Link__c, Internal_Title__c, Legal__c from Job_Number__c where ISCI__c <> null];
  
  Set<String> campaignSet = new Set<String>();
  for (Job_Number__c j : SPOT)
      campaignSet.add(j.Campaign__r.Name);
      
      for (String campaign : campaignSet) 
      {
            campaigns.add(campaign);
      }
      campaigns.sort();
    }
}

i forgot to initialize the list ! :)

All Answers

Ankit SehgalAnkit Sehgal
Using list instead of array would solve the problem:
public with sharing class AllSpotController
{

public List <Job_Number__c> SPOT {get;set;}
public list<String> campaigns {get;set;}

public void load() 
{

  SPOT = [Select id, name, Thumbnail_URL__c, ISCI__c, Title__c, Project__c,       Campaign__r.Name, Nets_Running__c, View_Link__c, Internal_Title__c, Legal__c from Job_Number__c where ISCI__c <> null];
  
  Set<String> campaignSet = new Set<String>();
  for (Job_Number__c j : SPOT)
      campaignSet.add(j.Campaign__r.Name);
      
      for (String campaign : campaignSet) 
      {
            campaigns.add(campaign);
      }
      campaigns.sort();
    }
}

 
John NeffJohn Neff
Thank you Ankit - I will give that a try!
John NeffJohn Neff
Hi Ankit - I tried using list instead of array - but am recieving an error 
Attempt to de-reference a null object
Error is in expression '{!load}' in component <apex:page> in page all_spots: Class.AllSpotController.load: line 18, column 1

Is there any chance that you could help me diagnose? 

Thanks, 

John
Ankit SehgalAnkit Sehgal
public with sharing class AllSpotController
{

public List <Job_Number__c> SPOT {get;set;}
public list<String> campaigns {get;set;}

public void load() 
{
  campaigns= new List<String>();
  SPOT = [Select id, name, Thumbnail_URL__c, ISCI__c, Title__c, Project__c,       Campaign__r.Name, Nets_Running__c, View_Link__c, Internal_Title__c, Legal__c from Job_Number__c where ISCI__c <> null];
  
  Set<String> campaignSet = new Set<String>();
  for (Job_Number__c j : SPOT)
      campaignSet.add(j.Campaign__r.Name);
      
      for (String campaign : campaignSet) 
      {
            campaigns.add(campaign);
      }
      campaigns.sort();
    }
}

i forgot to initialize the list ! :)
This was selected as the best answer
John NeffJohn Neff
Thanks Ankit!  I have the change in deployment now

If you could indulge me one more time?  Want is the differnce between the two?  I'd love to understand how this thing actually works!

John
John NeffJohn Neff
That worked beautifully!

Thank you!!!
Ankit SehgalAnkit Sehgal
List can be multidimensional

A "array notation" of a list using [] can only be one dimensional

Please refer below link:

http://salesforce.stackexchange.com/questions/5447/is-there-a-difference-between-an-array-and-a-list-in-apex/5450
John NeffJohn Neff
thanks!