delphi - A working way to sort a TListView column that contains a DateTime value -
i have tlistview wit 3 columns. column 1 text, column 2 text , column 3 datetime have following code on form
function comparetextasdatetime(const s1, s2: string): integer; begin result := comparedatetime(strtodatetime(s2), strtodatetime(s1)); end; procedure tfrmhsmailcollect.lstmailscolumnclick(sender: tobject; column: tlistcolumn); begin tlistview(sender).sorttype := stboth; end; procedure tfrmhsmailcollect.lstmailscompare(sender: tobject; item1, item2: tlistitem; data: integer; var compare: integer); begin compare := comparetextasdatetime(item1.subitems[coldate - 1], item2.subitems[coldate - 1]); compare := -compare; end;
the -compare because column needs sorted descending newest item first one. problem column isn't sorted. have been looking @ several solutions both here on website , others, looks same , none working.
so question is: correct way sort tlistview column contains datetime value.
the sorttype
property not need here. used automatic sorting. don't want that, want sort in response user action.
you instead need call customsort
method:
procedure tfrmhsmailcollect.lstmailscolumnclick(sender: tobject; column: tlistcolumn); begin tlistview(sender).customsort(nil, 0); end;
the first argument customsort
compare function. if nil
passed comparison routed oncompare
event, trust lstmailscompare
in code.
the second argument of customsort
passed compare method data
argument. not using mechanism , can pass like. use specify whether or not ordering should ascending or descending, instance.
Comments
Post a Comment