Here is what I use. I have "global" vars called $dir and $sortCol. $dir contains the current direction (asc or desc), while $sortCol contains the last sorted column. Using both, I then either reverse the sort is the column is the same as the current sorted column, or apply a asc sort if it is a new column.
sub lvwJobposts_ColumnClick{ my $sort = shift; #column to sort on, passed in by the click event and is the header you click on. my @Rows; for(my $x = 0; $x < $win->lvwJobposts->Count();$x++){ my @Values; for(my $y = 0; $y <= $win->lvwJobposts->{Columns}; $y++){ my %data = $win->lvwJobposts->ItemInfo($x, $y); push @Values, $data{-text}; } push @Rows, [EMAIL PROTECTED]; =pod this grabs all the columns in a row and stuffs into an array subscript, then go to the next array subscript and do the same thing again. After this loop, @Rows is a 2 diminsional array like this: [joe, frazier, 30, tammy], [john, smith, 35, ''], [bill, wilson, 40, Cary] where the columns are laid out like "firstname, lastname, age, wife". =cut } if ($sort == $sortCol){ if($dir eq 'f'){ $dir = 'b'; }elsif($dir eq 'b'){ $dir = 'f'; } }else{ $sortCol = $sort; $dir = "f"; } my @result; dosort ([EMAIL PROTECTED], [EMAIL PROTECTED], $sort, $dir); $win->lvwJobposts->Clear(); for (my $d = 0; $d <= $#result;$d++){ Win32::GUI::DoEvents(); $win->lvwJobposts->InsertItem(-text =>$result[$d]); } } sub dosort { # dosort [EMAIL PROTECTED], [EMAIL PROTECTED], column 0 to n, 'f'|'r' my ($sort, $sorted, $col, $dir) = @_; if ($dir eq 'f') { @$sorted = sort { $a->[$col] <=> $b->[$col] || $a->[$col] cmp $b->[$col] } @$sort; } else { @$sorted = sort { $b->[$col] <=> $a->[$col] || $b->[$col] cmp $a->[$col] } @$sort; } } This could be cleaned up and made shorter, but It works great and I dont have time to mess with it right now. Perhaps Aldo can put something in the core Win32 to handle all this junk for us at some point in the future. Joe > -----Original Message----- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] > Sent: Thursday, June 27, 2002 2:32 AM > To: perl-win32-gui-users@lists.sourceforge.net > 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 >