Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > sort problem

Reply
Thread Tools

sort problem

 
 
hugo leitmeier
Guest
Posts: n/a
 
      09-28-2003
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

 
Reply With Quote
 
 
 
 
Greg Bacon
Guest
Posts: n/a
 
      09-28-2003
In article <(E-Mail Removed)>,
hugo leitmeier <(E-Mail Removed)> 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 @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
--
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"
 
Reply With Quote
 
 
 
 
hugo leitmeier
Guest
Posts: n/a
 
      09-28-2003
Hi Greg,

thank you very much for your help.

best regards

hugo



Greg Bacon schrieb:
> In article <(E-Mail Removed)>,
> hugo leitmeier <(E-Mail Removed)> 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 @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



 
Reply With Quote
 
Michael Budash
Guest
Posts: n/a
 
      09-28-2003
In article <(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) (Greg Bacon) wrote:

> In article <(E-Mail Removed)>,
> hugo leitmeier <(E-Mail Removed)> 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 @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-

--
Michael Budash
 
Reply With Quote
 
Greg Bacon
Guest
Posts: n/a
 
      09-29-2003
In article <(E-Mail Removed)>,
Michael Budash <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: When will Thunderbird support sort in place (in context sort)? Ron Natalie Firefox 0 02-02-2006 04:38 AM
The Colourised Bewitched -- sort of OK....... sort of! anthony DVD Video 26 06-28-2005 04:39 AM
xsl:sort lang="es" modern vs. tradidional Spanish sort order nobody XML 0 06-01-2004 06:25 AM
What is faster? C++ vector sort or sort in database JerryJ C++ 11 04-28-2004 10:23 PM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments