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";
}


Reply via email to