Created
June 21, 2016 12:55
-
-
Save nicomen/f975cd400ef058ea06c29a125fc09082 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/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