diff --git a/lib/Subtitle/Format.pm b/lib/Subtitle/Format.pm index 470a89a..66da324 100644 --- a/lib/Subtitle/Format.pm +++ b/lib/Subtitle/Format.pm @@ -35,6 +35,14 @@ sub events { return wantarray ? @{ $self->{events} } : $self->{events}; } +sub sort_events { + my ($self) = @_; + my @sorted = sort { + $a->t_start <=> $b->t_start + } @{ $self->events; }; + $self->events(\@sorted); +} + sub from_string { my ($self, $text) = @_; my @lines = split /\r?\n/o, $text; @@ -102,6 +110,12 @@ Collected messages can be accessed like this: Access to subtitle events +=head2 C + + $obj->sort_events; + +Internally sort events by start time (ascending) + =head2 C =head2 C diff --git a/t/format.t b/t/format.t index 7f82571..0d917b6 100644 --- a/t/format.t +++ b/t/format.t @@ -1,7 +1,7 @@ use strict; use warnings; -use Test::More tests => 5; +use Test::More tests => 6; require_ok('Subtitle::Format'); @@ -21,6 +21,8 @@ require_ok('Subtitle::Format'); my $obj = Subtitle::Dummy->new; isa_ok($obj, 'Subtitle::Dummy'); +can_ok($obj, qw(new parse build log events sort_events + from_file to_file from_string to_string)); is(scalar @{ $obj->{log} }, 0); $obj->log(warn => "something");