in reply to Re^2: Challenge: sort weekdays in week-order (elegantly and efficiently)
in thread Challenge: sort weekdays in week-order (elegantly and efficiently)

++tybalt89!! That was a very clever use of "tr" and its options. I added a small tweak using unshift and pop to put Sunday first.

#!/usr/bin/perl use strict; use warnings; use List::AllUtils qw( shuffle sort_by ); my @weekdays = shuffle qw/Monday Tuesday Wednesday Friday Sunday Satur +day Thursday/; @weekdays = @{[ sort_by { lc =~ tr/muwhft/a-e/dr } @weekdays ]}; unshift(@weekdays,pop(@weekdays)); print "@weekdays\n";

"It's not how hard you work, it's how much you get done."

  • Comment on Re^3: Challenge: sort weekdays in week-order (elegantly and efficiently)
  • Download Code

Replies are listed 'Best First'.
Re^4: Challenge: sort weekdays in week-order (elegantly and efficiently)
by tybalt89 (Monsignor) on Jul 23, 2022 at 22:40 UTC

    No need for that, just tweak the "tr" a little :)

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11145633 use warnings; use List::AllUtils qw( shuffle sort_by ); my @weekdays = shuffle qw/Monday tuesday wednesday friday sunday Saturday Thursday/; print "$_\n" for sort_by { lc =~ tr/muwht/a-d/dr } @weekdays; # Monday + first print "\n"; print "$_\n" for sort_by { lc =~ tr/nmewhfatsu/a-g/dr } @weekdays; # S +unday first
      Should also work:
      print "$_\n" for sort_by { lc =~ tr/hfst/x-z/dr } @weekdays; # Monday +first print "\n"; print "$_\n" for reverse sort_by { scalar reverse lc =~ tr/rn/at/dr } +@weekdays; # Sunday first
      Nice! It just shifted from magic to deep magic. :)

      "It's not how hard you work, it's how much you get done."