Skip to content

Instantly share code, notes, and snippets.

@lkatney
Last active October 22, 2020 17:20
Show Gist options
  • Save lkatney/8f7e86899a1ec7a7e4f2 to your computer and use it in GitHub Desktop.
Save lkatney/8f7e86899a1ec7a7e4f2 to your computer and use it in GitHub Desktop.
Randomize a list through apex
public static list<wrapper> randomize(list<wrapper> lst){
integer currentIndex = lst.size();
wrapper temporaryValue;
integer randomIndex;
// While there remain elements to shuffle...
while (0 != currentIndex) {
// Pick a remaining element...
randomIndex = integer.valueOf(Math.floor(Math.random() * currentIndex));
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = lst[currentIndex];
lst[currentIndex] = lst[randomIndex];
lst[randomIndex] = temporaryValue;
}
return lst;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment