Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Sorting The Date format Values without using any perl modules.

by Corion (Pope)
on Dec 26, 2005 at 14:49 UTC ( #519149=note: print w/replies, xml ) Need Help??

in reply to Sorting The Date format Values without using any perl modules.

What did you try so far? Have you looked at Perls sort function? The documentation is available via perldoc -f sort, or perldoc sort. Did you try simply using sort? Please don't ask us to do your homework!

The sort documentation mentions the following example:

@articles = sort {$a cmp $b} @files;

Maybe you can adapt it to your needs.

Replies are listed 'Best First'.
Re^2: Sorting The Date format Values without using any perl modules.
by jesuashok (Curate) on Dec 26, 2005 at 15:30 UTC

    Please look into the above code. I tried using sort also. But then I am facing the Issue.

    #!/usr/bin/perl + + @sort = ( '05-11-2006', '01-01-2005' , '04-12-2005' , '22-03-2005'); + + foreach (sort @sort) { print ":$_:\n"; }
    OUTPUT: :01-01-2005: :04-12-2005: :05-11-2006: :22-03-2005:
    Please Note the output carefully. I want to that to be sorted. Please provide me your Idea.

    "Keep pouring your ideas"

      Did you read my node? Did you read the sort documentation? Did you look at my example and look how you could adapt it to your needs?

      Please take another look at the sort documentation. Please look at my example (taken from there) and see how you can adapt it to what you need.

      @articles = sort {$a cmp $b} @files;

        I tried your Suggestion and that too is giving wrong result. anyway thanks a lot for your answers.

        "Keep pouring your ideas"
      To add to Corion's comment the arrays in your original post and in your follow up are different.

      It's hard to help if the question keeps moving!

      update: the following is wrong :-(

      Each element in your follow up array is the reverse of those in the origninal. (There's a hint in there!)

      With that observation and another look at the docs I was able to sort the array in the order you're looking for using the method suggested by Corion.

      You didn't say my $sort = (... either.

      update 2:

      Pursuing the reverse theme, perhaps you could consider an intermediate step:

      #!/bin/perl5 use strict; use warnings; use Data::Dumper; my @sort = qw( 05-11-2006 01-01-2005 04-12-2005 22-03-2005 ); my @reversed = map { join '', substr($_, 6), substr($_, 3,2), substr($_, 0,2) } @sort; print Dumper(\@reversed); __DATA__ ---------- Capture Output ---------- > "C:\Perl\bin\perl.exe" $VAR1 = [ '20061105', '20050101', '20051204', '20050322' ]; > Terminated with exit code 0.

      Now you changed your requirements in the middle - which incidentally is mostly annoying. If they're as in the OP, then my reply still applies.

      Or else you will have to provide a suitable sort sub, possibly exploiting one of the various sorting techniques often references here and elsewhere. Try checking for example:

      from the Tutorials section. A recent post of mine using a form of Guttman-Rossler is this. In this case you should have fixed length fields and it should be even easier for you to recover the original data with a single substr.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://519149]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2017-02-21 01:32 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (305 votes). Check out past polls.