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
AbAb 

reiterating/looping a SelectOption in and comparing the order by a map

Hello,

I have a map with Name(String) and Ranks(Integer),
Name is the Pricebook name and Rank is Order by.
map<String,Integer> mapOfNameAndRank = new map<String,Integer>();

Name1, 1
Name2, 2
Name3, 3
I am populating SelectOption in below manner: 
public list<SelectOption> listOfOpt {get;set;}
for(priceBook2 prc : [select id,Name from priceBook2 ){
            listOfOpt.add(new SelectOption(prc.Id,prc.Name));
        }

My usecase is that, the SelectOption should display the Name is the order defined in map.

How can i reiterate the SelectOption inorder to order it.

thank you for suggestion
Best Answer chosen by Ab
Maharajan CMaharajan C
Hi Sandrine,

Try like below:
 
map<String,Integer> mapOfNameAndRank = new map<String,Integer>();
Name1, 1
Name2, 2
Name3, 3

public list<SelectOption> listOfOpt {get;set;}

Map<String,Id> pricebookMap = new Map<String,Id>();
for(priceBook2 prc : [select id,Name from priceBook2 ){
    pricebookMap.put(prc.Name , prc.Id);        
    }	
for(String key : mapOfNameAndRank.keySet())
{
	if(pricebookMap.containsKey(key)){
       listOfOpt.add(new SelectOption(pricebookMap.get(key), key));
   }
}

Thanks,
Maharajan.C

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Sandrine, 

What order do you get if you sort using the list.sort method  )The List.sort method sorts SelectOption elements in ascending order using the value and label fields, and is based on comparison sequence)

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_lists_sorting.htm

Or you can try using ORDER BY directly in your SOQL query. I ran the below example in my local org and it give me results in alphabetical order
SELECT Id, CaseNumber, Account.Id, Account.Name FROM Case ORDER BY Account.Name

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_orderby.htm

Anudeep
Maharajan CMaharajan C
Hi Sandrine,

Try like below:
 
map<String,Integer> mapOfNameAndRank = new map<String,Integer>();
Name1, 1
Name2, 2
Name3, 3

public list<SelectOption> listOfOpt {get;set;}

Map<String,Id> pricebookMap = new Map<String,Id>();
for(priceBook2 prc : [select id,Name from priceBook2 ){
    pricebookMap.put(prc.Name , prc.Id);        
    }	
for(String key : mapOfNameAndRank.keySet())
{
	if(pricebookMap.containsKey(key)){
       listOfOpt.add(new SelectOption(pricebookMap.get(key), key));
   }
}

Thanks,
Maharajan.C
This was selected as the best answer
sanu rajaksanu rajak
Nice Whatsapp status download now <a href=" https://fevers.website/category/love-whatsapp-status-video/">love status</a>
 
sanu rajaksanu rajak
Nice Whatsapp status download now <a href=" https://fevers.website/category/love-whatsapp-status-video/">love status</a>
love status (https://fevers.website/category/love-whatsapp-status-video/)