[EMAIL PROTECTED] wrote:
Mark,

Haven't had time to examine your code to closely but it looks as though
you're sorting everything as strings.  I assume the quantity should be a
number?

I've used something like the code below to try to deal with this.  I can
send you a working example if you need it and this is your problem.

Cheers,

Kev.

   sub alpascend {
             uc($details{$a}[$col]) cmp uc ($details{$b}[$col]);
   }

   sub numdescend {
             $details{$b}[$col] <=> $details{$a}[$col];
   }

   sub alpdescend {
           uc($details{$b}[$col]) cmp ($details{$a}[$col]);
   }

   sub initlv {
     $Window->ListView->Clear();
     my $rout;

     @index=keys %details;
     $rout= $details{1}[$col]=~ /^\d/ ? "num" : "alp";

     if ($dir==1) {
        $rout.="ascend";
     } else {
        $rout.="descend";
     }

     @index= sort $rout @index;

     foreach $i (@index) {
       if ($details{$i}[0] ne '..') {
          InsertListItem(0, $details{$i}[0], $details{$i}[1]);
       } else {
          InsertListItem(1, $details{$i}[0], $details{$i}[1]);
       }
     }
     $Window->ListView->SetFocus();
   }

   sub ListView_ColumnClick {
     $col=shift;
     if ($col==$lastcol) {
        $dir= $dir * -1;
     } else {
        $dir= -1;
     }
     initlv($dir);
     $lastcol=$col;
   }



|---------+------------------------------------------------>
|         |           [EMAIL PROTECTED]                     |
|         |           Sent by:                             |
|         |           [EMAIL PROTECTED]|
|         |           ceforge.net                          |
|         |                                                |
|         |                                                |
|         |           27/06/2002 07:32                     |
|         |                                                |
|---------+------------------------------------------------>
  
>----------------------------------------------------------------------------------------------|
  |                                                                             
                 |
  |       To:       perl-win32-gui-users@lists.sourceforge.net                  
                 |
  |       cc:                                                                   
                 |
  |       Subject:  [perl-win32-gui-users] sorting a listview                   
                 |
  
>----------------------------------------------------------------------------------------------|




hi. i've got alist view with 4 columns, being catalogue number, artist,
title and qty (there's 2 other columns but they don't matter) and i've been
trying to figure out how to sort the whole listview on a column click. i
found a message sometime last year where someone posted something that i
kind of got working, after a bit of tweaking. the problem is that the qty
column doesn't sort properly and i just can't seem to figure out why. all
the other columns sort fine when i click on them. any ideas? code below

thanx in advance


sub ListView_ColumnClick {
    $totalcols = 6;
    my $column = shift;
    print "column:$column\n";
    $column=$column+1;

    ## i do this so that I can toggle between ascending and descending
sorts
    ## 0 = ascending (A-Z), 1 = decending (Z - A)
     if ($lastcolumn == $column) # if you clicked the same column twice in
a row
          {$sortorder = 1 - $sortorder;}  # toggle between 1 and 0 values
     else
     {$sortorder = 0;}
     print "You Clicked $column, last time it was $lastcolumn,sortorder
=$sortorder\n";
     $lastcolumn = $column;

     %data=();
     $rows=$ListView->Count();
     print "rows:$rows\n";
     for $i(0..$rows-1)
          {
          $row="";
          my %result=$ListView->GetItem($i,0);
          $image=$result{-image};
          for $j(0..$totalcols-1)
               {
               my %result=$ListView->GetItem($i,$j);
               $text=$result{-text};
               $row.=",$text";
               }
          $data{$i}="$image$row";
         }

     my %sortcol = NewList($column, %data);
     SortListItem(\%data,\%sortcol,$column);
     if ($sortorder) {
         print "Sorted descending by Column $column\n";
     } else {
         print "Sorted ascending by Column $column\n";
     }
     return;
}

sub SortListItem {
   my ($data,$sortcol,$column) = @_;
   my $check;
   my %data = %$data;
   my %sortcol = %$sortcol;

   $check = "$_" foreach (values %sortcol);

   $ListView->Clear();  ## clear the ListView window
   $index = 0;
   if ($sortorder == 0)
          {  ## this is sorting in ascending order
          if (($column == 1) or ($column == 4))
               {
               print "sorting numerically\n";
                foreach (sort {
                 (substr($sortcol{$a},0,index($sortcol{$a}," "))) <=>
                     (substr($sortcol{$b},0,index($sortcol{$b}," "))) }
keys %sortcol)
                {
                 my @newdata = split/,/,$data{$_};
                 print $data{$_};
                 InsertListItem(@newdata);
                }
            }
          else {
              foreach (sort { uc($sortcol{$a}) cmp uc($sortcol{$b})
                        } keys %sortcol)
              {
               my @newdata = split/,/,$data{$_};
               print $data{$_};
               InsertListItem(@newdata);
              }
          }
          $ListView->Update();
         }
   else
   {   ## this is sorting in descending order
       if (($column == 1) or ($column == 4))
       {
        print "sorting numerically\n";
        foreach (sort {
            (substr($sortcol{$b},0,index($sortcol{$b}," "))) <=>
             (substr($sortcol{$a},0,index($sortcol{$a}," "))) } keys
%sortcol)
        {
            my @newdata = split/,/,$data{$_};
            print $data{$_};
            InsertListItem(@newdata);
        }
       }
       else {
        foreach (sort { uc($sortcol{$b}) cmp uc($sortcol{$a})
                  } keys %sortcol)
        {
            my @newdata = split/,/,$data{$_};
            print $data{$_};
            InsertListItem(@newdata);
        }
        $ListView->Update();
       }
   }
   my $lpPoint = pack("LLLLLALLLL", LVIF_STATE, 0, 0,
0x2000,LVIS_STATEIMAGEMASK, " ", 127, 0, 0, 0);
   my $rtn = $SendMsg->Call($ListView->{'-handle'}, LVM_SETITEMSTATE, -1,
$lpPoint);

   return;
}

sub NewList  {
    ## This creates another hash to use only for sorting purposes.
    my ($column,%sortcol) = @_;
    my $sortthis;

    foreach (keys %sortcol) {
     my @info = split /,/, $sortcol{$_};
     $sortthis = $info[$column];
     $sortcol{$_} = "$sortthis";
    }
    return(%sortcol);
}

sub InsertListItem {
    my(@info) = @_;
    $Window->ListView->InsertItem(
                      #-item  => $Window->ListView->Count(),
                      -text  => [EMAIL PROTECTED],@info[2],@info[3],@info[4],
@info[5],@info[6]],
                              );
    #$Window->ListView->SetItem(
#                     -item    => $item,
#                     -subitem => 1,
#                     -text    => $description,
#                     );
    print "@info[1] @info[2] @info[3] @info[4] @info[5] @info[6]\n";
}



-------------------------------------------------------
Sponsored by:
ThinkGeek at http://www.ThinkGeek.com/
_______________________________________________
Perl-Win32-GUI-Users mailing list
Perl-Win32-GUI-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users





-------------------------------------------------------
Sponsored by:
ThinkGeek at http://www.ThinkGeek.com/
_______________________________________________
Perl-Win32-GUI-Users mailing list
Perl-Win32-GUI-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users




kevin

if you're talking about using cmp instead of <=> then thats not it. depending on which column it is, it will use either of those 2. so for 1 and 4 (cat number and qty) it uses <=> and for anything else it uses cmp. sorting the cat number works perfectly but for qty it doesn't for some reason.

mark


Reply via email to