type
PSortInfo = ^TSortInfo;
TSortInfo = record
SortType:string;
Index:integer;
Ascending:boolean;
end;
procedure TFMainForm.SortListView(FLV:TListview;SortType:string;ColumnIndex: Integer; Ascending: Boolean);
var
af:TSortInfo;
function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
function Sign(Val: Extended): Integer;
begin
if Val < 0 then
Result := -1
else if Val > 0 then
Result := 1
else
Result := 0;
end;
begin
if UpperCase(PsortInfo(ParamSort)^.SortType)=UpperCase('date') then
begin
result:=Sign(StrToDateTime(item1.SubItems[PsortInfo(ParamSort)^.Index-1]) -
StrToDateTime(Item2.SubItems[PsortInfo(ParamSort)^.Index-1]));
end
else if UpperCase(PsortInfo(ParamSort)^.SortType)=UpperCase('Num') then
begin
result := Sign(StrToInt64(item1.SubItems[PsortInfo(ParamSort)^.Index-1])-
StrToint64(Item2.SubItems[PsortInfo(ParamSort)^.Index-1]));
end
else
result := AnsiCompareText(item1.SubItems[PsortInfo(ParamSort)^.Index-1],
Item2.SubItems[PsortInfo(ParamSort)^.Index-1]);
if not PsortInfo(ParamSort)^.Ascending then Result := -Result;
end;
begin
af.SortType := SortType;
af.Index := ColumnIndex;
af.Ascending := Ascending;
FLV.CustomSort(@CustomSortProc,Longint(@af));
end;
调用,如对列,文件名进行正序排
SortListView(listview1,'string',1,true);
反序排
SortListView(listview1,'string',1,false);
排时间列
SortListView(listview1,'date',2,false);
排数字列
SortListView(listview1,'Num',2,false);