How To Randomize List Order?


Does anyone have a clever way to output a bulleted list in randomized order?

@Sean_P_O_MacCath-Mor, can you tell me a bit more about how you would want to use this workflow please?

You bet!

Let's say you have a list of items, and you want to output them in a random order:

"The 30-Day Vegan Challenge":"",
"Challenge 22":"",
"One Ingredient Chef":"",
"The VeganStreet Guide for New Vegans":"",
"The Plantpower Meal Planner":""
{resource_keys=keys(resources)} <-- randomize resource_keys order somehow?
{repeat: for key in resource_keys}
-> {=key}: {=resources[key]}

It's a bit complex, but you can do it by sorting on a random number. I've implemented this in a reusable function below.

{randomizelist=(list) -> map(sort(map(list, x -> ["id":random(), "value":x]), (a,b) -> -, x -> x.value)}

{=randomizelist(["aaa", "bbb", "ccc", "ddd"])}
{=randomizelist(["aaa", "bbb", "ccc", "ddd"])}
{=randomizelist(["aaa", "bbb", "ccc", "ddd"])}
{=randomizelist(["aaa", "bbb", "ccc", "ddd"])}
{=randomizelist(["aaa", "bbb", "ccc", "ddd"])}

@scott, that is a bit of elegant genius, that is. Works a treat. Thank you VERY much!

1 Like