Skip to content

Instantly share code, notes, and snippets.

@tjmw
Last active November 18, 2015 12:33
Show Gist options
  • Select an option

  • Save tjmw/b7dd38dd30f8e2d791bb to your computer and use it in GitHub Desktop.

Select an option

Save tjmw/b7dd38dd30f8e2d791bb to your computer and use it in GitHub Desktop.
Deterministically sort an array based on the date
# requires active_support
list = [:foo, :bar, :baz]
# Today:
seed = Time.find_zone!('America/Los_Angeles').today.at_beginning_of_day.to_i
list.shuffle(random: Random.new(seed)) # => [:bar, :baz, :foo]
list.shuffle(random: Random.new(seed)) # => [:bar, :baz, :foo]
# Tomorrow:
seed = (Time.find_zone!('America/Los_Angeles').today + 1).at_beginning_of_day.to_i
list.shuffle(random: Random.new(seed)) # => [:baz, :foo, :bar]
list.shuffle(random: Random.new(seed)) # => [:baz, :foo, :bar]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment