I pulled the code below straight out of a program I wrote awhile back that
does this. This issue has been discussed several times, you might find
better examples in the archives. What most people have done is store their
data in a pre-defined hash, I actually pull the data directly from the table
and spit it back into the table.... Hope this helps, let me know if you need
any help understanding all this....

        Tim Thomas

sub ListView_ColumnClick {
        my $column = shift;
        $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;}
        #Win32::MsgBox("You Clicked $column, last time it was $lastcolumn,
sortorder=$sortorder", 0, "DEBUG");
        $lastcolumn = $column;

        %data=();
        $rows=$ListView->Count();
        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";
                #Win32::MsgBox("data($i)=$image$row\n");
                }

        my %sortcol = NewList($column, %data);
        SortListItem(\%data,\%sortcol,$column);
        if ($sortorder)
                {$Status->Text("Sorted descending by Column $column.");}
        else
                {$Status->Text("Sorted ascending by Column $column.");}
        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 == 2) or ($column == 3))
                        {
                        #Win32::MsgBox("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{$_};
                                InsertListItem(@newdata);
                                }
                        }
                else {
                      foreach (sort { uc($sortcol{$a}) cmp uc($sortcol{$b})
} keys %sortcol)
                                {
                                my @newdata = split/,/,$data{$_};
                                InsertListItem(@newdata);
                                }
                }
                $ListView->Update();
                }
   else
        {   ## this is sorting in descending order
                if (($column == 2) or ($column == 3))
                        {
                        #Win32::MsgBox("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{$_};
                                InsertListItem(@newdata);
                                }
                        }
                else {
                      foreach (sort { uc($sortcol{$b}) cmp uc($sortcol{$a})
} keys %sortcol)
                                {
                                my @newdata = split/,/,$data{$_};
                                InsertListItem(@newdata);
                                }
                        $ListView->Update();
                        }
        }
   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);
}

-----Original Message-----
From: Frazier, Joe Jr [mailto:[EMAIL PROTECTED]
Sent: Wednesday, September 05, 2001 11:18 AM
To: perl-win32-gui-users@lists.sourceforge.net
Subject: [perl-win32-gui-users] Sorting Listview


Is there a way to sort the data in a Listview control?  I know there is
a ColumnClick event for each column and this is where you would do your
sort, but has anyone already created a function to do this?  I would
assume you would have to load each row in to a data structure and use a
Schwartzian Transform, then rebuild the list?  Does this sound like the
right way to go or has anyone already done this?


Joe Frazier, Jr
Technical Support Engineer
PeopleClick
919-645-2916
[EMAIL PROTECTED]
 

_______________________________________________
Perl-Win32-GUI-Users mailing list
Perl-Win32-GUI-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users

Reply via email to