From: Frazier, J. J. <Joe.Frazier@Peopleclick.com> - 2005-02-23 12:58:42
|
Here is what I do in my Listview. I have a global var to keep track of the direction of the currently sorted column and one to keep track of the column currently sorted. Off the top of my head, I believe this will work for most fields. Of course, you might need a more general purpose convtoDate() if you use (or your database returns) a different date format(think: Wednesday, February 23, 2005. If using data from a database, my best suggestion (I wrote this app before I figured this out) is to use your databases sql formating functions to get an ISO date: 2005-02-23. This will always sort properly. Of course, this may not be an option for all databases and doesn't take other sources of dates into account such as command shell 'dir' lists, etc. If only dealing with a single date format, you could could also pass in a date format mask to the "custom" sort function. You could also use a Date/Time package such as DateManip, but that would introduce a dependency and would slow sorts down by half or more. sub lvwJobposts_ColumnClick{ my $sort =3D shift; my @Rows; for(my $x =3D 0; $x < $win->lvwJobposts->Count();$x++){ my @Values; for(my $y =3D 0; $y <=3D $win->lvwJobposts->{Columns}; $y++){ my %data =3D $win->lvwJobposts->ItemInfo($x, $y); push @Values, $data{-text}; } push @Rows, \@Values; } if ($sort =3D=3D $sortCol){ if($dir eq 'f'){ $dir =3D 'b'; }elsif($dir eq 'b'){ $dir =3D 'f'; } }else{ $sortCol =3D $sort; $dir =3D "f"; } my @result; dosort (\@Rows, \@result, $sort, $dir); $win->lvwJobposts->Clear(); for (my $d =3D 0; $d <=3D $#result;$d++){ Win32::GUI::DoEvents(); $win->lvwJobposts->InsertItem(-text =3D>$result[$d]); } } sub dosort { # dosort \@sort, \@result, column 0 to n, 'f'|'r' my ($sort, $sorted, $col, $dir) =3D @_; if ($dir eq 'f') { @$sorted =3D sort { convtoDate($a->[$col]) <=3D> convtoDate($b->[$col]) || $a->[$col] <=3D> $b->[$col] || $a->[$col] cmp $b->[$col] } @$sort; } else { @$sorted =3D sort { convtoDate($b->[$col]) <=3D> convtoDate($a->[$col]) || $b->[$col] <=3D> $a->[$col] || $b->[$col] cmp $a->[$col] } @$sort; } } sub convtoDate { my $val =3D shift; if ( $val =3D~ m|(\d+)/(\d+)/(\d+)|) { return sprintf "%4d%02d%02d", $3+2000,$1,$2; } return $val; } FYI: For MS SQL server 2000, I use something like: Select username, convert(char(10), createdon, 121) as [Createdon] From tbl_user This gets me : Username createdon --------------------------=20 Jfrazier 2005-02-23 Joe Frazier, Jr. Senior Support Engineer Peopleclick Service Support Tel: +1-800-841-2365 E-Mail: Support@Peopleclick.com=20 > -----Original Message----- > From: per...@li...=20 > [mailto:per...@li...] On=20 > Behalf Of Ariel Serbin > Sent: Tuesday, February 22, 2005 16:42 > To: per...@li... > Subject: [perl-win32-gui-users] wrappers revisited >=20 > I've been toying with the listview wrapper idea that I=20 > brought up last week. I threw together a module to show off=20 > the basic idea, though it needs to be reworked. I wanted to=20 > make it easy to set up a listview and keep track of the items=20 > by some id. When I started writing this, I used the=20 > hashref-of-hashrefs idea like DBI returns from=20 > fetchall_hashref() because it seemed like a good way of=20 > matching name/value pairs up with their respective ids. The=20 > problem with this is that there currently isn't a good way to=20 > sort the items in the listview because of the nature of hashing. >=20 > If you're interested in having a look, I have the rough=20 > module and a test script here: >=20 > http://www.serbin.org/ariel/mms.zip >=20 > I can easily fix this up to suit my immediate needs, but I=20 > think that it would be nice to make it more generic so that=20 > it would be helpful to others. If anyone has any opinions as=20 > to how this could be better implemented, please offer them up... >=20 > -Ariel >=20 >=20 >=20 >=20 > ------------------------------------------------------- > SF email is sponsored by - The IT Product Guide Read honest &=20 > candid reviews on hundreds of IT Products from real users. > Discover which products truly live up to the hype. Start reading now. > http://ads.osdn.com/?ad_id=3D6595&alloc_id=3D14396&op=3Dclick > _______________________________________________ > Perl-Win32-GUI-Users mailing list > Per...@li... > https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users >=20 |