不知道你用什么方法加载LISTVIEW,如果是直接listview.items.add这样的话,这个就可以了,如果是用ONDATA方式的另外还有一个。function ListViewCompare(I1, I2: TListItem; Data: Integer): Integer; stdcall; var V1, V2: string; function Sign(Val: Extended): Integer; begin if Val < 0 then Result := -1 else if Val > 0 then Result := 1 else Result := 0; end; //去除非数字字符,自己重写 function ExtractNum(const S: string): string; var i: Integer; begin Result := ''; for i := Length(S) downto 1 do begin if S[i] in ['0'..'9'] then Result := S[i] + Result; end; end;begin with PSortInfo(Data)^ do begin if Col = 0 then begin V1 := I1.Caption; V2 := I2.Caption; end else begin V1 := I1.SubItems[Col - 1]; V2 := I2.SubItems[Col - 1]; end; case Style of ssAlpha: Result := AnsiCompareText(V1, V2); ssNumeric: Result := Sign(StrToFloat(ExtractNum(V1)) - StrToFloat(ExtractNum(V2))); ssDateTime: Result := Sign(StrToDateTime(V1) - StrToDateTime(V2)); else Result := 0; end; if not Asc then Result := -Result; end; end; //== ListView 排序 ============================================================= procedure SortListView(ListView: TTEListView; ColumnIndex: Integer; Style: TSortStyle; Ascending: Boolean = True); // 参数说明 // ColumnIndex: 排序列索引号, // Style : 排序方式;(按字符,按数值,按日期) // (日期格式为SysUtils.ShortDataTimeFmt,缺省为YY-MM-DD); // Ascending : = True 按升序,否则按降序} var FSortInfo: TSortInfo; begin FSortInfo.Col := ColumnIndex; FSortInfo.Style := Style; FSortInfo.Asc := Ascending; ListView.CustomSort(@ListViewCompare, LongInt(@FSortInfo)); end;
-------------------------
???按列排序?按列的字母顺序???
添加的时候排阿...
var
V1, V2: string; function Sign(Val: Extended): Integer;
begin
if Val < 0 then
Result := -1
else if Val > 0 then
Result := 1
else
Result := 0;
end; //去除非数字字符,自己重写
function ExtractNum(const S: string): string;
var
i: Integer;
begin
Result := '';
for i := Length(S) downto 1 do
begin
if S[i] in ['0'..'9'] then
Result := S[i] + Result;
end;
end;begin
with PSortInfo(Data)^ do
begin if Col = 0 then
begin
V1 := I1.Caption;
V2 := I2.Caption;
end
else
begin
V1 := I1.SubItems[Col - 1];
V2 := I2.SubItems[Col - 1];
end; case Style of
ssAlpha: Result := AnsiCompareText(V1, V2);
ssNumeric: Result := Sign(StrToFloat(ExtractNum(V1)) - StrToFloat(ExtractNum(V2)));
ssDateTime: Result := Sign(StrToDateTime(V1) - StrToDateTime(V2));
else
Result := 0;
end; if not Asc then
Result := -Result;
end;
end;
//== ListView 排序 =============================================================
procedure SortListView(ListView: TTEListView; ColumnIndex: Integer;
Style: TSortStyle; Ascending: Boolean = True);
// 参数说明
// ColumnIndex: 排序列索引号,
// Style : 排序方式;(按字符,按数值,按日期)
// (日期格式为SysUtils.ShortDataTimeFmt,缺省为YY-MM-DD);
// Ascending : = True 按升序,否则按降序}
var
FSortInfo: TSortInfo;
begin
FSortInfo.Col := ColumnIndex;
FSortInfo.Style := Style;
FSortInfo.Asc := Ascending;
ListView.CustomSort(@ListViewCompare, LongInt(@FSortInfo));
end;
TSortStyle = (ssAlpha, ssNumeric, ssDateTime);
PSortInfo = ^TSortInfo;
TSortInfo = record
Col: Integer;
Style: TSortStyle;
Asc: Boolean;
end;