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
BARJRDBARJRD 

Sorting selected records in a standardsetcontroller

Is it possible to re-sort records that are selected on a list view and displayed on another page?  For example, I select 3 accounts from a listview, which are then displayed on a Mass Edit page.  I have tried several method to (in a controller extension) to re-sort the records without any luck.  Is there a way of using getRecords, sorting the records, then using setRecords to update the page display? Any ideas?

 

Thanks,

Barb R.

Best Answer chosen by Admin (Salesforce Developers) 
myforcedotcommyforcedotcom

Hi Barb,

 

Here are 2 possible ways of doing this.

 

Example 1would be grabbing the selected records off the list page, requering the db to get them in the sort order you like then making the sorted list the selected record of the controller

 

 

Example 1:

List<Accounts> acts =  new List<Accounts>;

acts = controller.getSelected();

Set <Id> aIds = new Set<Id>();

for(Account act : acts)

{

 aIds.add(act.Id);

}

 

acts = [Select Id, Name FROM Account where Id in:aIds Order By XXX];

controller.setSelected(acts);

 

Eaxmple 2:

 

List<String> sortlist = new List<string>

List<Accounts> acts =  new List<Accounts>;

 

acts = controller.getSelected();

for(Account act : acts)

{

 // add the column you want to sort by to the list

 sortList.add(act.Name)

}

 

sortList.sort();

List<Account> sortedActs = new List<Account>();

 

for(String sortString : sortList

{

 for(Account act : acts)

 {

 //check sorted string against account column you want to sort

 if(act.Name == sortString)

 {

 sortedActs.add(act);

 }

 }

}

 

controller.setSelected(sortedActs);

 

 

 

hope this helps

All Answers

myforcedotcommyforcedotcom

Hi Barb,

 

Here are 2 possible ways of doing this.

 

Example 1would be grabbing the selected records off the list page, requering the db to get them in the sort order you like then making the sorted list the selected record of the controller

 

 

Example 1:

List<Accounts> acts =  new List<Accounts>;

acts = controller.getSelected();

Set <Id> aIds = new Set<Id>();

for(Account act : acts)

{

 aIds.add(act.Id);

}

 

acts = [Select Id, Name FROM Account where Id in:aIds Order By XXX];

controller.setSelected(acts);

 

Eaxmple 2:

 

List<String> sortlist = new List<string>

List<Accounts> acts =  new List<Accounts>;

 

acts = controller.getSelected();

for(Account act : acts)

{

 // add the column you want to sort by to the list

 sortList.add(act.Name)

}

 

sortList.sort();

List<Account> sortedActs = new List<Account>();

 

for(String sortString : sortList

{

 for(Account act : acts)

 {

 //check sorted string against account column you want to sort

 if(act.Name == sortString)

 {

 sortedActs.add(act);

 }

 }

}

 

controller.setSelected(sortedActs);

 

 

 

hope this helps

This was selected as the best answer
BARBAR

Thanks :-)