function ListViewRowSort(mListView: TListView; mByte: integer; mDesc: Boolean =
False): Boolean;
function ListViewRowSwap(mListView: TListView; mFromRow, mToRow: Integer):
Boolean;
var
S: string;
begin
Result := False;
if (mToRow = mFromRow) then
Exit;
if not Assigned(mListView) then
Exit;
if (mFromRow < 0) or (mFromRow >= mListView.Items.Count) then
Exit;
if (mToRow < 0) or (mToRow >= mListView.Items.Count) then
Exit;
try
S := mListView.Items.Item[mFromRow].Caption;
mListView.Items.Item[mFromRow].Caption :=
mListView.Items.Item[mToRow].Caption;
mListView.Items.Item[mToRow].Caption := S;
except
Exit;
end;
Result := True;
end;
var
I, J: Integer;
begin
Result := False;
if not Assigned(mListView) then
Exit;
for I := 0 to mListView.Items.Count - 2 do
for J := I + 1 to mListView.Items.Count - 1 do
if mDesc then
if RightStr(mListView.Items.Item[I].Caption, mByte) <
RightStr(mListView.Items.Item[J].Caption, mByte) then
ListViewRowSwap(mListView, I, J)
else
else if RightStr(mListView.Items.Item[I].Caption, mByte) >
RightStr(mListView.Items.Item[J].Caption, mByte) then
ListViewRowSwap(mListView, I, J);
Result := True;
end;
//参数说明?:ListView,从第几字节开始排序,升序or降序
procedure TForm1.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
begin
if ColumnToSort <> Column.Index then
IsAscSort := True
else
IsAscSort := not IsAscSort;
ColumnToSort := Column.Index;
(Sender as TCustomListView).AlphaSort;
end;
procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
Data: Integer; var Compare: Integer);
var
ix: Integer;
begin
if ColumnToSort = 0 then
if IsAscSort then
Compare := CompareText(Item1.Caption, Item2.Caption)
else
Compare := CompareText(Item2.Caption, Item1.Caption)
else begin
ix := ColumnToSort - 1;
if IsAscSort then
Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix])
else
Compare := CompareText(Item2.SubItems[ix],Item1.SubItems[ix]);
end;
end;