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
kamal3883kamal3883 

Sorting in map

HI all,

 

I want to do sorting of map based on value. here is code:

 

for (Pricebook2 pb : [select id, Name from Pricebook2 where id in :ids order by name])
        {
            myPBNames.put(pb.id, pb.name);
        }

 

Results are got sorted based on id. But i want this should be based on name.

 

Even Order by is not working. Please suggest.

souvik9086souvik9086

Check this

 

http://boards.developerforce.com/t5/Apex-Code-Development/Sorting-a-Map-by-Value/td-p/297945

 

 If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

kamal3883kamal3883

i checked this one. But didnt get proper solution. Is it possible to use order by in query so that results are sorted.

souvik9086souvik9086

Yes, but you are telling that the previous code is not ordering by name.

 

Check this if it helps

http://sforcetips.blogspot.in/2013/05/apex-map-sorting.html

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

kamal3883kamal3883

when i check values of map by accmap.get(key). it shows sorted values but when i try to print accmap. Order is still same.

kamal3883kamal3883

same issue: map shows values in same order.

vbsvbs
@Kamal - This is the characteristic of a Map - sort order of elements is not guaranteed. You will have to build a map of name, Id pairs. Then build a list from the map.keySet. Sort this list and then iterate through it. The most obvious flaw with this approach is the assumption that the name is unique. The other alternative is to build your own sorting algorithm while iterating through the map.
kamal3883kamal3883

All values are unique so no issue with that. I tried this portion of code. when i print simply values of map it shows sorted values. But when i try to print full maporder is still.

 

Is there some other way that i can sort it in java script. Please provide some code

My original map is MyPBNames

 

 

MAP<string , List<String>>  reverseMap =  new MAP<string , List<String>>();

List<String> tempL = new List<String>();
for(String k : myPBNames.KeySet())
          {
                 if(reverseMap.containsKey(myPBNames.get(k)))      
                { 
                        
                       tempL =reverseMap.get(myPBNames.get(k));
               }  
               else
                 tempL = new List<String>();
          
              tempL.add(k);
              reverseMap.put(myPBNames.get(k) , tempL);
           }



List<String> aList = new List<String>();
aList.addAll(myPBNames.keySet());
List<string> value= new List<string>();
Set<String> key = new Set<String>();
for(String s : aList)
{
value.add(myPBNames.get(s));
}
value.sort();

List<string> descValue = new List<string>();
for(Integer i=value.size()-1; i>=0;i--)
{
descValue.add(value.get(i));
}

List<String> sortedKey = new List<String>();
for(string d : descValue)
{
         sortedKey.addAll(reverseMap.get(d));  
}

//I have debug values coming out of map are in reverse sorted error
system.debug('*************** Sorted KEy : '+ sortedKey);
for(String finalK : sortedKey)
  {
         System.debug('***************   Fianl Ordered MAP '+ myPBNames.get(finalK));
        // myPBNames1.put(finalk,myPBNames.get(finalK));
  }
 
//end sorting

 

 

ArunaAruna
Hi ,

Are you able to get a workaround for this Map<id,Name>.

I need same issue solution I want to sort by Name. Please post me the solution if you have anything.

Thank you,
Aruna.