1

I have code below

public class LearnAboutMap {
     public LearnAboutMap () {
          Map<String, String> m = new Map<String, String>();
          m.put('Type', '1');
          m.put('Contact', '1');
          m.put('Additional', '1');
          m.put('Supper', '1');
          m.put('Body', '1');
          System.debug(m);

          //Output: {Additional=1, Body=1, Contact=1, Supper=1, Type=1}
          //Expected: {Type=1, Contact=1, Additional=1, Supper=1, Body=1}
     }

}

Why Map in Apex is auto sort key ?

2
  • Out of curiosity, why does it matter? Commented May 22, 2019 at 4:51
  • @Ohana I want to show map in visualforce page position extractly Commented May 22, 2019 at 4:55

4 Answers 4

3

Since you want Map keys to be iterated in the order you placed in the Map. So convert the Map.keySet() to List and it will be sorted.

Map<String, String> m = new Map<String, String>();
m.put('Type', '1');
m.put('Contact', '1');
m.put('Additional', '1');
m.put('Supper', '1');
m.put('Body', '1');
System.debug(new List<String>(m.keySet()));

12:04:32:003 USER_DEBUG [7]|DEBUG|(Type, Contact, Additional, Supper, Body)

2

Not sure if this answers your question, but here's what the docs say on how they are implemented:

Apex uses a hash structure for all maps, and the The iteration order of map elements is deterministic. You can rely on the order being the same in each subsequent execution of the same code. However, SF recommends to always access map elements by key.

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

2
  • I want to put any key and value exactly position but it always sorted by key Commented May 22, 2019 at 4:54
  • If that’s the result you want , then don’t use a map. Use a list of something.... given the example above, use a list of maps, where each list element has a map of 1 key value pair. Commented May 22, 2019 at 12:32
2

You're looking at the internal stringified representation of the map that is made available to you for debug purposes. The Map class does not specify the order of keys, there's no "auto sort". Deterministic ordering is only guaranteed on map's elements via values() method.

2
  • When I show this map in visualforce page by <apex:repeat />. It's the same output in system.debug. I don't understand this issue. Commented May 22, 2019 at 4:49
  • You might want to amend your question to mention apex:repeat or post a new one. salesforce.stackexchange.com/questions/34582/…
    – identigral
    Commented May 22, 2019 at 5:25
-1

System.debug sorts values. Use:

for (String key : myMap.keyset()) {
System.debug(key);
}

And you will see values as they were put into a Map.

Since comments below suggest that I am wrong please run this code as anonymous apex:

Map<String, String> users = new Map<String,String>();

users.put('Mike', 'Mith');
users.put('Adam', 'Adler');
users.put('Zenon', 'Ziber');
users.put('Peter', 'Portland');

System.debug('users.keySet() passed to System.debug is sorted A to Z.');
System.debug(users.keySet());

System.debug('Iterating over users.keySet() give us records in insert order.');
for (String firstName : users.keySet()) {
    System.debug(firstName);
}

With API v59 I am getting such result:

11:54:00.28 (29524300)|USER_DEBUG|[8]|DEBUG|users.keySet() passed to System.debug is sorted A to Z.
11:54:00.28 (29642752)|USER_DEBUG|[9]|DEBUG|{Adam, Mike, Peter, Zenon}
11:54:00.28 (29659913)|USER_DEBUG|[11]|DEBUG|Iterating over users.keySet() give us records in insert order.
11:54:00.28 (29846066)|USER_DEBUG|[13]|DEBUG|Mike
11:54:00.28 (29867216)|USER_DEBUG|[13]|DEBUG|Adam
11:54:00.28 (29878237)|USER_DEBUG|[13]|DEBUG|Zenon
11:54:00.28 (29887848)|USER_DEBUG|[13]|DEBUG|Peter
4
  • It's rather Map's toString() implementation, isn't it? Try System.debug(myMap.toString()). The Apex System.debug is a far simpler animal than Javascript's console.log(). Commented Jan 23 at 8:50
  • 1
    In addition to that, your answer seems to suggest insertion order. From what I can tell, Salesforce doesn't guarantee insertion order. Salesforce guarantees the continuity of a once set order. Insertion order is just gleaned from observations, not something to rely on. Or have I overlooked anything? Commented Jan 23 at 9:08
  • Updated post with code example which you can run as anonymous apex. From Summer 15 order of inserting items for Maps, Set and List is kept. salesforce23.wordpress.com/2015/08/29/… Commented Jan 24 at 11:01
  • Insertion order is not the same as predictability. Please point me to Salesforce documentation that states keys in Salesforce's Maps keep insertion order. All what Salesforce guarantees is that the (any) order is kept. And this is certainly not rebutted by finding that in many cases Salesforce keeps the insertion order. Commented Jan 24 at 15:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .