I am trying to add values to a list iterating through a for loop like this.

List<String> leftvalues = new List<String>();
for(OpportunityLineItem  o: OLI_List)
    //Gets product name from map and adds to list

Strangely its sorting internally after adding instead of adding at the end of list each time. What am i doing wrong .

Debug log results:

05:29:24.463 (463698000)|USER_DEBUG|[77]|DEBUG|***Leftvalues:{17TBUPGRADE}

05:29:24.464 (464053000)|USER_DEBUG|[77]|DEBUG|***Leftvalues:{17TBUPGRADE, 5UPB-C10/W}

05:29:24.464 (464397000)|USER_DEBUG|[77]|DEBUG|***Leftvalues:{17TBUPGRADE, 5UPB-C10/W, IFU-PN100M}

05:29:24.464 (464739000)|USER_DEBUG|[77]|DEBUG|***Leftvalues:{17TBUPGRADE, 5UPB-C10/W, CBK-UPG01, IFU-PN100M}

Why did new value CBK-UPG01 go to third position instead of fourth.?

How to prevent it from re positioning itself within the list ? Am i doing something wrong ?

  • can you share the whole method? – Shebin Mathew Dec 30 '13 at 11:24
  • 4
    Your code looks ok to me, but what I'm starting to wonder is if you can rely on the conversion from list<string> -> string when you are printing it with System.debug. Could you verify after the for-loop that the values are infact out of order (by for-looping through the list)? – haagen Dec 30 '13 at 11:35
  • 1
    agree with @haagen – Shebin Mathew Dec 30 '13 at 11:42
  • 1
    Lists are guaranteed to be ordered, also when casting to String. Will you extend debugging by logging o value as well? I bet the issue is with the order of OLI_List. – endrju Dec 30 '13 at 14:12

The code that you've provided doesn't match the debug log. Maybe someone concurrently changed the code while you were running it, or you're just testing us ;-) It doesn't really matter - the question is a great puzzle!

Let's have a quick look at the documentation. First of all, Lists are guaranteed to be ordered and the method add always puts an element at the end of a List. The described behavior would mean a serious bug in the Salesforce List implementation, that has been used by almost every APEX program for ages with no complains.

Even more important clue is given the kind of brackets in the debug log. The specification of String.valueOf suggests that Lists are serialized using parentheses ( ), and a quick test shows that curly brackets { } are reserved for Sets.

Conclusion: the debug log presents an algorithm using a Set. By the way it's worth noting that in Salesforce Sets are implemented using hashes and do not guarantee any order (but it might happen that Set values are sorted when casting to String).

| improve this answer | |
  • Good catch - I learned something new here! – haagen Dec 30 '13 at 21:33
  • Such a good answer. Such a shame the OP didn't take the time to accept your answer. – Programmable Medley Apr 16 '15 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.