in reply to Sorting and Grouping with two different fields

vcTheGuru,
This is actually an interesting sort order. If you were going to do this in perl, it would look something like (untested):
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"; } }
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.

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

    Thanks for the perl Solution. but . . .

    I am getting the error "Can't coerce array into hash at" for my $entry (sort {$a->[0] <=> $b->[0]} values %{$data{$state}{entry}}) I googled it and refered "Can't coerce array into hash at", but couldnt fix it. Anybody please help!

    Thanks In advance for the help

    --VC

      vcTheGuru,
      As I stated, the code was untested. It worries me that you are testing code you don't understand and that you can't generate your own code using my description of the algorithm. I believe the solution is:
      for my $entry (sort {$a->[0] <=> $b->[0]} @{$data{$state}{entry}}) { # ... }

      Cheers - L~R

        Hi Limbic~Region,

        Thanks for the reply. I tested this code earlier also, but didnt get answer properly. ie there is a bug in logic where we assigns to the hash. The output we get is

         1 Virginia Norfolk
         2 Virginia Chesapeake
         3 Virginia Virginia Beach
         6 Virginia Falls Church
         8 Washington Seatle
         9 Washington Spokane
         4 Indiana Evansville
         5 Indiana Fort Wayne
         7 Indiana Indianapolis


        This is NOT the o/p I need. So I am still trying to get it correct. (didnt gaveup yet)

        "It worries me that you are testing code you don't understand"

        Its another way how I am learn. I try to change the code and learn all possibilities :)

        Thanks for your help & reply

        -- VC