Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to sort a file with dependencies? 2

Status
Not open for further replies.
Apr 30, 2003
56
US
I have a file reads something like this:
01-JAN-2007, USER=crystal
02-JAN-2007, USER=crystal
02-JAN-2007, USER=Phillips
02-JAN-2007, USER=robinson
02-JAN-2007, USER=maguilar
02-JAN-2007, USER=cshannon

I want to sort the file with username first then the date.

My script as the following:
#!/usr/bin/perl
open (DISPLAY, display.txt)||die "Can't open the file!\n";
@line=<DISPLAY>;
@line=sort(@line);
print "@line";
close(DISPLAY);

However, the results is not I wanted. My results came out like this:
01-APR-2007, USER=crystal
01-APR-2008, USER=SMSMaster
01-APR-2008, USER=aaghili
01-APR-2008, USER=crystal
01-APR-2008, USER=elemm
01-APR-2008, USER=feathers
01-APR-2008, USER=maduro
01-APR-2008, USER=maguilar

It does sort, but it seems like it sorts the date first, even though it sorts the date, it is not the kind of sort I want. How can I achieve the sort I want which will be username first and then the date? Thanks in advance for your help.
 
Have a look at Sorting with the Schwartzian Transform for a better explanation, but this should get you started:
Code:
chomp(my @input = <DATA>);
my @sorted = map {$_->[0]} sort {$a->[1] cmp $b->[1] || $a->[0] cmp $b->[0]}
				map {[$_, (split("=", $_))[-1]]} @input;
print "$_\n" for @sorted;

__DATA__
01-JAN-2007, USER=crystal
02-JAN-2007, USER=crystal
02-JAN-2007, USER=Phillips
02-JAN-2007, USER=robinson
02-JAN-2007, USER=maguilar
02-JAN-2007, USER=cshannon
You may need to change the sort in order to ignore case (if that is something you want.)
 
Thanks rharsh for your help. Your sample code does a great job of sorting the users first. However, the dates are still sorted wrong. Somehow the months are sorted by their alphabetical letters rather than the actual sequential month in orders. Is there a special sort function to sort the dates?
 
The dates are not sortable by date in the format of your file. They have to be converted to YYYYMMDD (all numbers with leading zeros) to sort them or use one of the date modules or Time::Local to convert the dates into epoch seconds to sort them.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I guess you could also do it in one go.
The code is quite short, though I doubt if it is very efficient with all the substringing going on.
However, it does work... ;-)

Code:
use strict;

# Sample input
my @input = ("01-JAN-2007, USER=crystal",
             "02-JAN-2007, USER=crystal",
             "12-JAN-2007, USER=Phillips",
             "01-APR-2007, USER=crystal",
             "12-JAN-2007, USER=crystal",
             "01-APR-2008, USER=SMSMaster",
             "01-APR-2008, USER=aaghili",
             "01-APR-2008, USER=crystal",
             "01-APR-2008, USER=elemm",
             "01-APR-2008, USER=feathers",
             "01-APR-2008, USER=maduro",
             "01-APR-2008, USER=maguilar",
             "02-JAN-2007, USER=robinson",
             "02-JAN-2007, USER=maguilar",
             "02-JAN-2007, USER=cshannon" );

# Make a hash mapping months to numbers
my %months = (JAN => 1, FEB => 2, MAR => 3, APR => 4, MAY => 5, JUN => 6,
              JUL => 7, AUG => 8, SEP => 9, OCT => 10, NOV => 11, DEC => 12);

# Sort everything in one go
my @sorted = sort {substr(lc($a), 18) cmp substr(lc($b), 18) ||
                    substr($a, 7, 4) <=> substr($b, 7, 4) ||
                     $months{substr($a, 3, 3)} <=> $months{substr($b, 3, 3)} ||
                      substr($a, 0, 2) <=> substr($b, 0, 2) } @input;

print "$_\n" for @sorted;

The output is:
Code:
01-APR-2008, USER=aaghili
01-JAN-2007, USER=crystal
02-JAN-2007, USER=crystal
12-JAN-2007, USER=crystal
01-APR-2007, USER=crystal
01-APR-2008, USER=crystal
02-JAN-2007, USER=cshannon
01-APR-2008, USER=elemm
01-APR-2008, USER=feathers
01-APR-2008, USER=maduro
02-JAN-2007, USER=maguilar
01-APR-2008, USER=maguilar
12-JAN-2007, USER=Phillips
02-JAN-2007, USER=robinson
01-APR-2008, USER=SMSMaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top