Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Sorting dates with the Schwartzian Transform

by Anonymous Monk
on Aug 03, 2011 at 13:45 UTC ( #918256=note: print w/ replies, xml ) Need Help??


in reply to Sorting dates with the Schwartzian Transform

Do I have to convert this column before the ST to a number, sort it during the ST as a number, and after the ST convert it back to the DD-MM-YYYY format?

Um no, just do a schwartzian-transform ;)

@dates = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { my $f = $_; s/\D//g; [ $f, $_ ] } @dates;


Comment on Re: Sorting dates with the Schwartzian Transform
Download Code
Re^2: Sorting dates with the Schwartzian Transform
by GotToBTru (Curate) on Aug 03, 2011 at 17:16 UTC

    Schwartzian, no doubt, but not enough transformation. The dates are originally in DD-MM-YYYY format and must be rearranged for effective sorting.

    @dates = map { $_->[0] } sort { $a->[1] >= $b->[1] } map { my $f = $_; /(\d\d)-(\d\d)-(\d{4})/g; [ $f, "$3$2$1" ] } @dates;

    Also, incomplete. There are 3 other columns that should figure in the sort. I'd like to see the OP's code.

    On a side note, I had never heard of the ST before. I looked briefly in perlsyn to see how I would know that the map {} sort {} map {} would be executed in reverse order, but I didn't find it. Any clues?

      ... how I would know that the map {} sort {} map {} would be executed in reverse order, but I didn't find it. Any clues?

      Both map and sort take a list (which is on the RHS), do some transformation of it then return the transformed list (to the LHS).

      my @mapped = map { # some transform code } @unmapped; my @sorted = sort { # sorting code } @unsorted;

      The ST code is just an extension of this right-to-left pattern:

      • the first map extracts the sorted dates in the original DD-MM-YYYY format and assigns to the @dates array on the LHS of the assignment operator (=);

      • but it can't do that before the sort has evaluated, sorting the items;

      • which in turn can't do any sorting before the bottom (or rightmost) map has transformed some dates into something sort can work with, taking its raw material from the rightmost part of the expression which is the original, unsorted @dates array.

      I hope this makes things a bit clearer.

      Cheers,

      JohnGG

        From: Schwartzian Transform
        19: #Knowing the context of certain operations 20: # and being able to chain them together 21: # is crucial to a deep and idiomatic 22: # understanding of Perl.

        True dat.

        Thanks to those who took the time. Some nice pointers to resources I was unaware of.

        Except for the pointer to Google search. That was uncool ;).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-12-19 22:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (94 votes), past polls