From 4c88c47a4bac711f57c2fd7f8f7a2302a334b9c2 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Tue, 29 Jun 2021 12:59:57 +0200 Subject: simplify event sorting using the Schwartzian Transform --- bin/events2md.pl | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/bin/events2md.pl b/bin/events2md.pl index 84880af..847b7b9 100755 --- a/bin/events2md.pl +++ b/bin/events2md.pl @@ -22,7 +22,7 @@ use Cal::DAV; use Data::ICal; use iCal::Parser; use List::Util qw(first); -use List::MoreUtils qw(nsort_by sort_by); +use List::MoreUtils qw(nsort_by qsort); use DateTime; use Try::Tiny; use Path::Tiny; @@ -161,21 +161,20 @@ for my $year ( map { $_->value } { for my $month ( map { $_->value } nsort_by { $_->key } pairs %$year ) { for my $day ( map { $_->value } nsort_by { $_->key } pairs %$month ) { - for ( - sort_by { - $_->value->{DTSTART} - . $_->value->{DTEND} - . ( $_->value->{SUMMARY} || '' ) - } - pairs %$day - ) - { - print_event( - $calendar_entries{VEVENT}{ $_->key }, - $_->value->{DTSTART}, - $_->value->{DTEND}, - $output_path, - ); + my @events = map { + [ $calendar_entries{VEVENT}{$_}, + $day->{$_}{DTSTART}, $day->{$_}{DTEND} + ] + } keys %$day; + qsort { + DateTime->compare( $a->[1], $b->[1] ) + || DateTime->compare( $a->[2], $b->[2] ) + || get_property_string( $a->[0], 'summary' ) + cmp get_property_string( $b->[0], 'summary' ) + } + @events; + for (@events) { + print_event( $_->[0], $_->[1], $_->[2], $output_path, ); } } } -- cgit v1.2.3