delphi StringList date sort

function CompareDates(List: TStringList; Index1, Index2: Integer): Integer;
var
d1, d2: TDateTime;
AFormat, AFormat2: TFormatSettings;
begin
AFormat.DateSeparator := ‘/’;
AFormat.ShortDateFormat := ‘yyyy/mm/dd’;
AFormat2.DateSeparator := ‘-’;
AFormat2.ShortDateFormat := ‘yyyy-mm-dd’;
if Pos(‘/’, LeftStr(List[Index1],10)) > 0 then
d1 := StrToDate(LeftStr(List[Index1],10),AFormat);
if Pos(‘/’, LeftStr(List[Index2],10)) > 0 then
d2 := StrToDate(LeftStr(List[Index2],10),AFormat);
if Pos(‘-’, LeftStr(List[Index1],10)) > 0 then
d1 := StrToDate(LeftStr(List[Index1],10),AFormat2);
if Pos(‘-’, LeftStr(List[Index2],10)) > 0 then
d2 := StrToDate(LeftStr(List[Index2],10),AFormat2);

if d1 < d2 then
Result := -1
else if d1 > d2 then Result := 1
else
Result := 0;
end;

自定義排序:

DateList.CustomSort(CompareDates);

 

ref:

http://www.swissdelphicenter.ch/en/showcode.php?id=1664

http://hi.baidu.com/caihexi/item/e99ab1154df6e70b8fbde425

This entry was posted in Program and tagged . Bookmark the permalink.

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>