Skip to content

Instantly share code, notes, and snippets.

@nicomen
Created June 21, 2016 12:55
Show Gist options
  • Save nicomen/f975cd400ef058ea06c29a125fc09082 to your computer and use it in GitHub Desktop.
Save nicomen/f975cd400ef058ea06c29a125fc09082 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -l
use strict;
use warnings;
use Mojo::Util;
use Mojo::Collection;
Mojo::Util::monkey_patch('Mojo::Collection', sort_by_order => sub {
my ($self, $order, $cb) = @_;
local $_ = { map { $order->[$_] => $_ } 0..@{$order}-1 };
return $self->sort( $cb );
});
print Mojo::Collection->new ( { a => 2 }, { a => 1 }, { a =>3 } )
->sort_by_order( [ 3, 2, 1 ], sub { $_->{$a->{a}} <=> $_->{$b->{a}} } )
->map ( sub { $_->{a} } )
->join ( ', ' );
# 3, 2, 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment