Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi. i'm trying to sort a win32::gui listview when a column header is clicked and it works wonderfully for columns 1,2 and 3, but column 4 it just won't sort properly (yes column 4 are numbers). i've been over it like a million times and i just can't see where it's going wrong. any ideas? (code below)

sub ListView_ColumnClick { $totalcols = 6; #5 and 6 are hidden my $column = shift; $column=$column+1; if ($lastcolumn == $column) { $sortorder = 1 - $sortorder; } else { $sortorder = 0; } %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"; } 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)) { 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 %so +rtcol) { my @newdata = split/,/,$data{$_}; InsertListItem(@newdata); } } } else { ## this is sorting in descending order if (($column == 1) or ($column == 4)) { 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 %so +rtcol) { my @newdata = split/,/,$data{$_}; InsertListItem(@newdata); } } } return; } sub NewList { 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( -text => [@info[1],@info[2],@info[3],@info[4],@info +[5],@info[6]], ); }

Replies are listed 'Best First'.
Re: sorting a listview
by talwyn (Monk) on Jul 04, 2002 at 18:45 UTC
    Well I set-up a quick test .... basically one of your sort mechanisms seems to not be working.

    I commented out the offending code and the code appears to work now. I also made some minor changes to accomodate my wrapper for variables and control names.

    Since Strings and numbers are scalars you don't need to differentiate them( i think ) they can be directly compared and will sort out. I tested with random numbers, random characters and combinations it worked for all colummns so I added a column and it still worked.

    I didn't examine the faulty code to trace the reason it didn't work but if you want root cause you have a starting point.