sort problem

H

hugo leitmeier

Hi,

Problem:

Date Name Average Number
---- ---- ------- ------
21.09.2003 Caesar 17.75 3748
15.08.2003 Anton 10.34 276
10.08.2003 Berta 15.38 138
27.08.2003 Anton 12.00 2765
29.08.2003 Anton 11.25 2766
10.08.2003 Caesar 10.00 374
12.08.2003 Caesar 15.75 3748



I like that. But, how can i accomplish that?

Date Name Average Number
---- ---- ------- ------
15.08.2003 Anton 10.34 276
27.08.2003 Anton 12.00 2765
29.08.2003 Anton 11.25 2766
10.08.2003 Berta 15.38 138
10.08.2003 Caesar 10.00 374
12.08.2003 Caesar 15.75 3748
21.09.2003 Caesar 17.75 3748
---------- ------ ----- ----
3rd (asc) 1st(asc) 2nd(asc)

best regards

hugo
 
G

Greg Bacon

: [...]
: I like that. But, how can i accomplish that?
:
: Date Name Average Number
: ---- ---- ------- ------
: 15.08.2003 Anton 10.34 276
: 27.08.2003 Anton 12.00 2765
: 29.08.2003 Anton 11.25 2766
: 10.08.2003 Berta 15.38 138
: 10.08.2003 Caesar 10.00 374
: 12.08.2003 Caesar 15.75 3748
: 21.09.2003 Caesar 17.75 3748
: ---------- ------ ----- ----
: 3rd (asc) 1st(asc) 2nd(asc)

% cat try
#! /usr/local/bin/perl

use warnings;
use strict;

my @header;
my @rows;

while (<DATA>) {
if (1 .. /^----/) {
push @header => $_;
}
else {
push @rows => $_;
}
}

print @header,
map $_->[0],
sort {
$a->[2] cmp $b->[2] ||
$a->[3] <=> $b->[3] ||
$a->[1] cmp $b->[1]
}
map [$_, (split)[0,1,3]],
@rows;

__DATA__
Date Name Average Number
---- ---- ------- ------
21.09.2003 Caesar 17.75 3748
15.08.2003 Anton 10.34 276
10.08.2003 Berta 15.38 138
27.08.2003 Anton 12.00 2765
29.08.2003 Anton 11.25 2766
10.08.2003 Caesar 10.00 374
12.08.2003 Caesar 15.75 3748
% ./try
Date Name Average Number
---- ---- ------- ------
15.08.2003 Anton 10.34 276
27.08.2003 Anton 12.00 2765
29.08.2003 Anton 11.25 2766
10.08.2003 Berta 15.38 138
10.08.2003 Caesar 10.00 374
12.08.2003 Caesar 15.75 3748
21.09.2003 Caesar 17.75 3748

Hope this helps,
Greg
 
M

Michael Budash

: [...]
: I like that. But, how can i accomplish that?
:
: Date Name Average Number
: ---- ---- ------- ------
: 15.08.2003 Anton 10.34 276
: 27.08.2003 Anton 12.00 2765
: 29.08.2003 Anton 11.25 2766
: 10.08.2003 Berta 15.38 138
: 10.08.2003 Caesar 10.00 374
: 12.08.2003 Caesar 15.75 3748
: 21.09.2003 Caesar 17.75 3748
: ---------- ------ ----- ----
: 3rd (asc) 1st(asc) 2nd(asc)

% cat try
#! /usr/local/bin/perl

use warnings;
use strict;

my @header;
my @rows;

while (<DATA>) {
if (1 .. /^----/) {
push @header => $_;
}
else {
push @rows => $_;
}
}

print @header,
map $_->[0],
sort {
$a->[2] cmp $b->[2] ||
$a->[3] <=> $b->[3] ||
$a->[1] cmp $b->[1]
}
map [$_, (split)[0,1,3]],
@rows;

__DATA__
Date Name Average Number
---- ---- ------- ------
21.09.2003 Caesar 17.75 3748
15.08.2003 Anton 10.34 276
10.08.2003 Berta 15.38 138
27.08.2003 Anton 12.00 2765
29.08.2003 Anton 11.25 2766
10.08.2003 Caesar 10.00 374
12.08.2003 Caesar 15.75 3748

% ./try
Date Name Average Number
---- ---- ------- ------
15.08.2003 Anton 10.34 276
27.08.2003 Anton 12.00 2765
29.08.2003 Anton 11.25 2766
10.08.2003 Berta 15.38 138
10.08.2003 Caesar 10.00 374
12.08.2003 Caesar 15.75 3748
21.09.2003 Caesar 17.75 3748

Hope this helps,
Greg

close, but col one is not sorting by date, which i assume the o.p.
wanted. it just looks like it is. you can see this if you change the
date on the last record thusly:

__DATA__
Date Name Average Number
---- ---- ------- ------
21.09.2003 Caesar 17.75 3748
15.08.2003 Anton 10.34 276
10.08.2003 Berta 15.38 138
27.08.2003 Anton 12.00 2765
29.08.2003 Anton 11.25 2766
10.08.2003 Caesar 10.00 374
12.10.2003 Caesar 15.75 3748

your results:

Date Name Average Number
---- ---- ------- ------
15.08.2003 Anton 10.34 276
27.08.2003 Anton 12.00 2765
29.08.2003 Anton 11.25 2766
10.08.2003 Berta 15.38 138
10.08.2003 Caesar 10.00 374
12.10.2003 Caesar 15.75 3748
21.09.2003 Caesar 17.75 3748

a slight change to your pre-sort map will fix this. additionally, i
rearranged the order of that map to make it (i think) easier to see what
the sort is doing:

print @header,
map $_->[0],
sort {
$a->[1] cmp $b->[1] ||
$a->[2] <=> $b->[2] ||
$a->[3] <=> $b->[3] ||
$a->[4] <=> $b->[4] ||
$a->[5] <=> $b->[5]
}
map [
$_, # the whole record
(split)[1,3], # Name, Number
(split (/\./, (split)[0]))[2,1,0] # Date yyyy, mm, dd
],
@rows;

hth-
 
G

Greg Bacon

: close, but col one is not sorting by date [...]

Oh crud, good catch. Three cheers for YYYY-MM-DD!

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top