unit OrderListView;
interface
uses
SysUtils, Classes, Controls, ComCtrls;
type
TOrderListView = class(TListView)
private
{ Private declarations }
protected
procedure ColClick(Column: TListColumn); override;
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
public
procedure OnMyColumnClick(Sender: TObject; Column: TListColumn);
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TOrderListView]);
end;
{ TOrderListView }
procedure TOrderListView.ColClick(Column: TListColumn);
begin
OnMyColumnClick(Self, Column);
inherited;
end;
procedure TOrderListView.OnMyColumnClick(Sender: TObject;
Column: TListColumn);
var
iRowNo:integer;//行数
iColumnNo:integer;//列数
i,j,m:integer;
str:array of TStringList;
smallimagine:array of Integer;
imagTmp:Integer;
isAsc:boolean;
isNumber:boolean;
stp:string;
v,code:integer;
begin
iRowNo:=Items.Count;
iColumnNo:=Columns.Count;
//创建图标的数组
SetLength(smallimagine,iRowNo);
//创建数组
SetLength(str,iColumnNo);
for i := 0 to iColumnNo - 1 do
begin
str[i]:=TStringList.Create;
end;
//将listview1控件的值传给变量str,将图标序列传给smallimagine
for i := 0 to iRowNo - 1 do
begin
smallimagine[i]:=Items[i].ImageIndex;
str[0].Append(Items[i].Caption);
for j := 0 to (iColumnNo -2) do
begin
str[j + 1].Append(Items[i].SubItems[j]);
end;
end;
isNumber:=true;
for i := 0 to iRowNo - 1 do
begin
stp:=str[Column.Index].Strings[i];
val(stp,v,code);
if code <> 0 then
begin
isNumber:=false;
break;
end;
end;
//查找当前标题是升序还是降序排列
isAsc:=true;
if (Pos('↑', Column.Caption) > 0) then
begin
isAsc:=false;
end
else if (Pos('↓', Column.Caption) > 0) then
begin
isAsc:=true;
end;
//去掉 '↑' 或者'↓'字符
for i:=0 to Columns.Count - 1 do
begin
stp:= Columns.Items[i].Caption;
if (Pos('↑', stp) > 0) or (Pos('↓', stp) > 0) then
begin
Columns.Items[i].Caption:=copy(stp,3,length(stp) - 2);
end;
end;
//排序(数值按降序排列,字符按升序排列)
if (isNumber = true) then //数值
begin
if isAsc= true then
begin
{***************************************}
//数字排序_升序
for i:=0 to str[Column.Index].Count - 1 do
begin
for j := 0 to str[Column.Index].Count - 1 do
begin
if i=j then continue;
if (StrToInt64(str[Column.Index][i]) < StrToInt64(str[Column.Index][j])) then
begin
for m:=0 to iColumnNo - 1 do
begin
str[m].Exchange(i,j);
imagTmp:=smallimagine[i];
smallimagine[i]:=smallimagine[j];
smallimagine[j]:=imagTmp;
end;
end;
end;
end;
Column.Caption:='↑' + Column.Caption;
{***************************************}
end
else
begin
{***************************************}
//数字排序_降序
for i:=0 to str[Column.Index].Count - 1 do
begin
for j := 0 to str[Column.Index].Count - 1 do
begin
if (StrToInt64(str[Column.Index][i]) > StrToInt64(str[Column.Index][j])) then
begin
for m:=0 to iColumnNo - 1 do
begin
str[m].Exchange(i,j);
imagTmp:=smallimagine[i];
smallimagine[i]:=smallimagine[j];
smallimagine[j]:=imagTmp;
end;
end;
end;
end;
Column.Caption:='↓' + Column.Caption;
{***************************************}
end;
end
else //字符
begin
if isAsc= true then
begin
{***************************************}
//字符排序_升序
for i:=0 to str[Column.Index].Count - 1 do
begin
for j := 0 to str[Column.Index].Count - 1 do
begin
if (str[Column.Index][i] < str[Column.Index][j]) then
begin
for m:=0 to iColumnNo - 1 do
begin
str[m].Exchange(i,j);
imagTmp:=smallimagine[i];
smallimagine[i]:=smallimagine[j];
smallimagine[j]:=imagTmp;
end;
end;
end;
end;
Column.Caption:='↑' + Column.Caption;
{***************************************}
end
else
begin
{***************************************}
//字符排序_降序
for i:=0 to str[Column.Index].Count - 1 do
begin
for j := 0 to str[Column.Index].Count - 1 do
begin
if (str[Column.Index][i] > str[Column.Index][j]) then
begin
for m:=0 to iColumnNo - 1 do
begin
str[m].Exchange(i,j);
imagTmp:=smallimagine[i];
smallimagine[i]:=smallimagine[j];
smallimagine[j]:=imagTmp;
end;
end;
end;
end;
Column.Caption:='↓' + Column.Caption;
{***************************************}
end;
end;
//排序完成
//将str的内容写回到ListView1
Items.Clear;
for i := 0 to iRowNo - 1 do
begin
with Items.Insert(i) do
begin
Caption:=Str[0].Strings[i];
for j := 0 to (iColumnNo -2) do
begin
SubItems.Add(str[j+1].Strings[i]);
end;
end;
Items[i].ImageIndex:=smallimagine[i];
end;
//释放数组
for i:= 0 to iColumnNo - 1 do
begin
str[i].Free;
end;
SetLength(smallimagine,0);
end;
end.