Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Challenge: sort weekdays in week-order (elegantly and efficiently)

by haukex (Archbishop)
on Jul 22, 2022 at 14:10 UTC ( [id://11145658]=note: print w/replies, xml ) Need Help??


in reply to Challenge: sort weekdays in week-order (elegantly and efficiently)

I agree with the others that using a module like Time::Piece, DateTime, or at least Sort::Key is the right way to go about this. But just for fun, TIMTWOTDI, and to fulfill your "some kind of transform" idea:

use List::Util qw/shuffle/; my @weekdays = shuffle qw/ Monday Tuesday Wednesday Thursday Friday Saturday Sunday /; say join ", ", map { $$_[0] } sort { $$a[1] cmp $$b[1] } map { [$_, lc(substr $_,0,2)=~tr/softwarehum/50411307860/r] } @weekdays;

Update: Anagram'd

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

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

    Inspired by above post.

    #!/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 "@{[ sort_by { lc =~ tr/muwhft/a-e/dr } @weekdays ]}\n";
      ++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."

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11145658]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-23 16:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found