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 = shift;
        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];
        }
        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 { convtoDate($a->[$col]) <=>
convtoDate($b->[$col]) || $a->[$col] <=> $b->[$col] || $a->[$col] cmp
$b->[$col] } @$sort;
        } else {
                @$sorted = sort { convtoDate($b->[$col]) <=>
convtoDate($a->[$col]) || $b->[$col] <=> $a->[$col] || $b->[$col] cmp
$a->[$col] } @$sort;
        }
}

sub convtoDate
{
        my $val = shift;
        if ( $val =~ 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
-------------------------- 
Jfrazier                2005-02-23

Joe Frazier, Jr.
Senior Support Engineer

Peopleclick Service Support
Tel:  +1-800-841-2365
E-Mail: [EMAIL PROTECTED] 

> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On 
> Behalf Of Ariel Serbin
> Sent: Tuesday, February 22, 2005 16:42
> To: perl-win32-gui-users@lists.sourceforge.net
> Subject: [perl-win32-gui-users] wrappers revisited
> 
> I've been toying with the listview wrapper idea that I 
> brought up last week.  I threw together a module to show off 
> the basic idea, though it needs to be reworked.  I wanted to 
> make it easy to set up a listview and keep track of the items 
> by some id.  When I started writing this, I used the 
> hashref-of-hashrefs idea like DBI returns from 
> fetchall_hashref() because it seemed like a good way of 
> matching name/value pairs up with their respective ids.  The 
> problem with this is that there currently isn't a good way to 
> sort the items in the listview because of the nature of hashing.
> 
> If you're interested in having a look, I have the rough 
> module and a test script here:
> 
> http://www.serbin.org/ariel/mms.zip
> 
> I can easily fix this up to suit my immediate needs, but I 
> think that it would be nice to make it more generic so that 
> it would be helpful to others.  If anyone has any opinions as 
> to how this could be better implemented, please offer them up...
> 
> -Ariel
> 
> 
> 
> 
> -------------------------------------------------------
> SF email is sponsored by - The IT Product Guide Read honest & 
> 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=6595&alloc_id=14396&op=click
> _______________________________________________
> 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