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
:) :) :) :) :):) :) :) :) :) 

Shuffle or Randomize a List

How can I shuffle a List Collection in APEX.

 

Ex: List<Account> acc = new List<Account>({Acc1, Acc2, Acc3,....,AccN});

 

acc.shuffle(); or acc.random(); // I want something that shuffle or randomize the primitive or sobject data inside the list.

 

output should be ==> {Acc2048, Acc179, Acc8632,.....,AccN} 

 

Thanks in advance. 

 

Vinita_SFDCVinita_SFDC

Hello,

 

There is no random/shuffle method in apex but we can make use of random methid of maths class and can get a random number like in the below link:

 

http://wordgraphs.com/post/2303/Getting-random-number-in-Salesforce-Apex

 

http://stackoverflow.com/questions/14203851/salesforce-com-apex-test-class-issue-need-to-hard-code-opportunity-custom-field