Last active
October 22, 2020 17:20
-
-
Save lkatney/8f7e86899a1ec7a7e4f2 to your computer and use it in GitHub Desktop.
Randomize a list through apex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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