# Is there a way to optimize this?

i tried to implement something below.

A B C
if i choose B the list should be in the order of B C A
if i choose C the list should be in the order of C A B

``````String FirstToBeAssignDealer = 'b';
Map<String, Integer> DealerScoreA = new Map<String, Integer>{'a' => 1, 'b' => 2, 'c' => 3};
List<string> DealerScoreB = new List<string>();
Map<String,Integer> DealerScorec = new map<String,Integer>();
List<String> dealerIdList = new List<string>();
//ReArrange Map of dealerscore based on FirstToBeAssignDealer
String nextDealerID = FirstToBeAssignDealer;
for(integer i=0;i<dealerIdList.size();i++)
{
system.debug(dealerIdList[i] == nextDealerID);
if(dealerIdList[i] == nextDealerID)
{
if(i != dealerIdList.size()-1) nextDealerID = dealerIdList[i+1];
}
}
system.debug(DealerScoreB);
for(integer i=0;i<dealerIdList.size();i++)
{
if(dealerIdList[i] == FirstToBeAssignDealer) break;
}
system.debug(DealerScoreB);
``````

//output b c a

I think using a list here will be a more easy and effective way. So we just need to shift the elements back and forth. Basically, the map doesn't maintain the order but list do.

Here is my solution to this, provided that the input list is sorted as per need. In this example, I am considering the alphabetical order.

I have written the below class for this. You can even make this class generic to work with any type of list.

``````public class RoundRobinList {
public static List<String> perform(String choice, List<String> inputList){
Integer ind = inputList.indexOf(choice);
String[] outputList = new List<String>();
if(ind > 0 ){
for(Integer i=ind+1; i < inputList.size(); i++){
}

for(Integer i=0; i < ind; i++){
}
return outputList;
}
return inputList;
}
}
``````

I tried below test cases.

``````List<String> inputList = new List<String>{ 'a', 'b', 'c', 'd'};
System.debug('Original List => ' + inputList);
System.debug('Input choice = b & OutputList => ' + RoundRobinList.perform('b', inputList));
System.debug('Input choice = c & OutputList => ' + RoundRobinList.perform('c', inputList));
System.debug('Input choice = d & OutputList => ' + RoundRobinList.perform('d', inputList));
System.debug('Input choice = a & OutputList => ' + RoundRobinList.perform('a', inputList));
``````

And here is the output.

Here is the method you can use:

``````public static Map<String, Integer> check() {
String splitKey = 'c';
Map<String, Integer> newDealerScore = new Map<String, Integer>();
Map<String, Integer> DealerScoreA = new Map<String, Integer>{'a' => 1, 'c' => 3, 'b' => 2, 'e'=>5, 'd'=>4};
List<String> sortedKeys = new List<String>(DealerScoreA.keySet());
sortedKeys.sort();
system.debug('before processing => '+sortedKeys);

for(Integer i=sortedKeys.indexOf(splitKey),s=sortedKeys.size(); i<s; i++){
newDealerScore.put(sortedKeys.get(i), DealerScoreA.get(sortedKeys.get(i)));
}
for(Integer i=0,s=sortedKeys.size(); i<s; i++){
newDealerScore.put(sortedKeys.get(i), DealerScoreA.get(sortedKeys.get(i)));
}

system.debug('after processing => '+new List<String>(newDealerScore.keySet()));
return newDealerScore;
}
``````

Steps:

1. Get the keys into list and sort them.
2. Get the index of selected key.
3. Implement 2 for loops - 1 from selected index to full size, 2nd from 0th to < selected index and put elements in both loops one after the other.