in reply to Sorting and Grouping with two different fields
Basically, you keep a low water mark for each state. Order the states by their lowest id and then for each entry for that state by id in ascending order.my %data; while (my @col = $dbh->fetchrow_array) { if (exists $data{$col[1]}) { $data{$col[1]}{min} = $col[0] if $col[0] < $data{$col[1]}{min} +; push @{$data{$col[1]}{entry}}, \@col1; } else { $data{$col[1]}{min} = $col[0]; $data{$col[1]}{entry} = [\@col]; } } for my $state (sort {$a->{min} <=> $b->{min}} keys %data) { for my $entry (sort {$a->[0] <=> $b->[0]} values %{$data{$state}{e +ntry}}) { print "@$entry\n"; } }
Cheers - L~R
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sorting and Grouping with two different fields
by atemon (Chaplain) on Jul 12, 2007 at 16:00 UTC | |
by Limbic~Region (Chancellor) on Jul 12, 2007 at 19:25 UTC | |
by atemon (Chaplain) on Jul 13, 2007 at 04:48 UTC | |
by Limbic~Region (Chancellor) on Jul 15, 2007 at 01:24 UTC |