# sort problem

Discussion in 'Perl Misc' started by hugo leitmeier, Sep 28, 2003.

1. ### hugo leitmeierGuest

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

hugo leitmeier, Sep 28, 2003

2. ### Greg BaconGuest

In article <>,
hugo leitmeier <> wrote:

: [...]
: 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 @rows;

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

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
--
Government, even in its best state, is but a necessary evil; in its worst
state, an intolerable one. Government, like dress, is the badge of lost
innocence; the palaces of kings are built upon the ruins of the bowers of
paradise. -- Thomas Paine, "Common Sense"

Greg Bacon, Sep 28, 2003

3. ### hugo leitmeierGuest

Hi Greg,

thank you very much for your help.

best regards

hugo

Greg Bacon schrieb:
> In article <>,
> hugo leitmeier <> wrote:
>
> : [...]
> : 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 @rows;
>
> while (<DATA>) {
> if (1 .. /^----/) {
> push @header => \$_;
> }
> else {
> push @rows => \$_;
> }
> }
>
> 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

hugo leitmeier, Sep 28, 2003
4. ### Michael BudashGuest

In article <>,
(Greg Bacon) wrote:

> In article <>,
> hugo leitmeier <> wrote:
>
> : [...]
> : 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 @rows;
>
> while (<DATA>) {
> if (1 .. /^----/) {
> push @header => \$_;
> }
> else {
> push @rows => \$_;
> }
> }
>
> 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

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:

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-

--
Michael Budash

Michael Budash, Sep 28, 2003
5. ### Greg BaconGuest

In article <>,
Michael Budash <> wrote:

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

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

Greg
--
Moreover, if a free market were to be enacted in both intrastate and
interstate markets, then the politicians and bureaucrats would find
themselves in that most terrible (for them) condition: irrelevance.
-- William L. Anderson

Greg Bacon, Sep 29, 2003