Skip to content

Instantly share code, notes, and snippets.

@libitte
Created October 31, 2013 11:49
Show Gist options
  • Save libitte/7248412 to your computer and use it in GitHub Desktop.
Save libitte/7248412 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use Perl6::Say;
use Data::Dumper;
#use List::Util qw( shuffle );
use Digest::MD5 qw(md5_hex);
my $KEY = 'bra-bra';
my $KEY_NUM = 10;
my @box_items = (
{ id => 1, box_id => 1, item_id => 100, class => 0, weight => 3 },
{ id => 2, box_id => 1, item_id => 110, class => 0, weight => 3 },
{ id => 3, box_id => 1, item_id => 120, class => 0, weight => 3 },
{ id => 4, box_id => 1, item_id => 130, class => 0, weight => 3 },
{ id => 5, box_id => 1, item_id => 200, class => 1, weight => 1 },
);
my $user_id = 10_000;
my $n = @box_items;
say $n;
my $i = 0;
for my $box_item (@box_items) {
$box_item->{sort_key} = substr(md5_hex(join(":", $user_id+$i, $KEY)), 0, $KEY_NUM);
$i++;
}
say Dumper [sort { $a->{sort_key} cmp $b->{sort_key} } @box_items];
#print Dumper [ shuffle (@box_items) ];
__END__
5
$VAR1 = [
{
'weight' => 3,
'box_id' => 1,
'class' => 0,
'id' => 4,
'sort_key' => '6d63bc605b',
'item_id' => 130
},
{
'weight' => 3,
'box_id' => 1,
'class' => 0,
'id' => 2,
'sort_key' => '8bce641942',
'item_id' => 110
},
{
'weight' => 3,
'box_id' => 1,
'class' => 0,
'id' => 3,
'sort_key' => '8d49b76d31',
'item_id' => 120
},
{
'weight' => 1,
'box_id' => 1,
'class' => 1,
'id' => 5,
'sort_key' => 'a1a0316a2f',
'item_id' => 200
},
{
'weight' => 3,
'box_id' => 1,
'class' => 0,
'id' => 1,
'sort_key' => 'd6fa06749f',
'item_id' => 100
}
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment