Tim,

here goes:

NOTE:  DataView is the name of my listview that I have set up.  My data is
also read in from a comma delimited file and stored in the listview.

=================================================

sub DataView_ColumnClick {
   my $column = shift;

## 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;
   }
   $lastcolumn = $column;
   my %sortcol = &NewList($column, %data);
   &SortListItem(\%data,\%sortcol);
   return;
}

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

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

   $MainWindow->DataView->Clear();  ## clear the ListView window
   $index = 0;
   if ($sortorder == 0) {  ## this is sorting in ascending order
      foreach (sort { uc($sortcol{$a}) cmp uc($sortcol{$b}) } keys %sortcol)
{
         my @newdata = split/,/,$data{$_};
         ($fname,$lname,$build,$room,$adap,$ip,$year,$os,$type) = @newdata;
# our list of items to sort
         if ($fname ne "") {
            $MainWindow->DataView->InsertItem(-item => $index, -subitem=>
0, -text => "$fname", -image => $img);
            $MainWindow->DataView->SetItem(-item => $index, -subitem =>
1, -text => "$lname");
            $MainWindow->DataView->SetItem(-item => $index, -subitem =>
2, -text => "$build");
            $MainWindow->DataView->SetItem(-item => $index, -subitem =>
3, -text => "$room");
            $MainWindow->DataView->SetItem(-item => $index, -subitem =>
4, -text => "$adap");
            $MainWindow->DataView->SetItem(-item => $index, -subitem =>
5, -text => "$ip");
            $MainWindow->DataView->SetItem(-item => $index, -subitem =>
6, -text => "$year");
            $MainWindow->DataView->SetItem(-item => $index, -subitem =>
7, -text => "$os");
            $MainWindow->DataView->SetItem(-item => $index, -subitem =>
8, -text => "$type");
            ++$index;
         }
         $MainWindow->DataView->Update();
      }
   }
   else {   ## this is sorting in descending order
      foreach (sort { uc($sortcol{$b}) cmp uc($sortcol{$a}) } keys %sortcol)
{
         my @newdata = split/,/,$data{$_};
         ($fname,$lname,$build,$room,$adap,$ip,$year,$os,$type) = @newdata;
         if ($fname ne "") {
            $MainWindow->DataView->InsertItem(-item => $index, -subitem =>
0, -text => "$fname", -image => $img);
            $MainWindow->DataView->SetItem(-item => $index, -subitem =>
1, -text => "$lname");
            $MainWindow->DataView->SetItem(-item => $index, -subitem =>
2, -text => "$build");
            $MainWindow->DataView->SetItem(-item => $index, -subitem =>
3, -text => "$room");
            $MainWindow->DataView->SetItem(-item => $index, -subitem =>
4, -text => "$adap");
            $MainWindow->DataView->SetItem(-item => $index, -subitem =>
5, -text => "$ip");
            $MainWindow->DataView->SetItem(-item => $index, -subitem =>
6, -text => "$year");
            $MainWindow->DataView->SetItem(-item => $index, -subitem =>
7, -text => "$os");
            $MainWindow->DataView->SetItem(-item => $index, -subitem =>
8, -text => "$type");
            ++$index;
         }
         $MainWindow->DataView->Update();
      }
   }
   $MainStatus->Text($tempstatus);
   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);
}

===================================================

That should be all you need.  If you have any more questions i will TRY to
answer them.


Jonathan Southwick
Technical & Network Services
Allegheny College, Meadsville, PA
[EMAIL PROTECTED]


----- Original Message -----
From: "Thomas, Timothy B" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>; <perl-win32-gui-users@lists.sourceforge.net>
Sent: Friday, January 12, 2001 3:42 PM
Subject: [perl-win32-gui-users] Listview column click sorting


> Jonathon,
> Can you post the code that you refer to in this old posting. I am looking
for the code that allows you to click on a column header and sort by that
field.
> http://www.mail-archive.com/[EMAIL PROTECTED]/msg00280.html
> Thanks,
> Tim Thomas
>
>
>
>
>
> --------------------------------------------------------------------------
-----------------------
> Tim Thomas
> Unix Systems Administrator
> Lockheed Martin EIS ยท Denver Data Center
> 303-430-2281
> mailto:[EMAIL PROTECTED]
> --------------------------------------------------------------------------
-----------------------
>
>
> _______________________________________________
> Perl-Win32-GUI-Users mailing list
> Perl-Win32-GUI-Users@lists.sourceforge.net
> http://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users
>


Reply via email to