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

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
  • Comment on Re^4: Challenge: sort weekdays in week-order (elegantly and efficiently)
  • Download Code

Replies are listed 'Best First'.
Re^5: Challenge: sort weekdays in week-order (elegantly and efficiently)
by rsFalse (Chaplain) on Jul 25, 2022 at 22:51 UTC
    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
Re^5: Challenge: sort weekdays in week-order (elegantly and efficiently)
by roho (Bishop) on Jul 24, 2022 at 02:06 UTC
    Nice! It just shifted from magic to deep magic. :)

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