类定义如下:
TVectorGraph = class
private
FGraphicsList: TList;
...
public
procedure Sort;
...
end; 两个排序相关的函数:
function CompareFunc(Item1, Item2: Pointer): Integer;
function CompareInt(int1, int2: int64): Integer;
函数实现:
procedure TVectorGraph.Sort();
begin
FGraphicsList.Sort(CompareFunc);
end;function CompareFunc(Item1, Item2: Pointer): Integer;
begin
Result := CompareInt(TGraphics(Item1^).ObjectId, TGraphics(Item2^).ObjectId);
end;function CompareInt(int1, int2: int64): Integer;
begin
if int1 > int2 then
Result :=1
else
if int1 < int2 then
Result :=-1
else
Result :=0;
end; 大家帮忙看一下我这样做有问题么?有的话问题在哪里?
怎么做更合适?
TVectorGraph = class
private
FGraphicsList: TList;
...
public
procedure Sort;
...
end; 两个排序相关的函数:
function CompareFunc(Item1, Item2: Pointer): Integer;
function CompareInt(int1, int2: int64): Integer;
函数实现:
procedure TVectorGraph.Sort();
begin
FGraphicsList.Sort(CompareFunc);
end;function CompareFunc(Item1, Item2: Pointer): Integer;
begin
Result := CompareInt(TGraphics(Item1^).ObjectId, TGraphics(Item2^).ObjectId);
end;function CompareInt(int1, int2: int64): Integer;
begin
if int1 > int2 then
Result :=1
else
if int1 < int2 then
Result :=-1
else
Result :=0;
end; 大家帮忙看一下我这样做有问题么?有的话问题在哪里?
怎么做更合适?
可以定义一个指针,也可以取指针内容,比如:
var
pMy_Point:^Integer;
i ,j: integer
begin
i := 9;
pMy_Pointer := @i;//或者Addr(i);//此时pMy_Pointer指向i的内存区域;
j := pMy_Pointer^;//此时j = i;
end;
begin
Result := CompareInt(TGraphics(Item1^).ObjectId, TGraphics(Item2^).ObjectId);
//上面这句改成如下试试?
//Result := CompareInt(TGraphics(Item1)^.ObjectId, TGraphics(Item2)^.ObjectId);
end;
编译器提示类型不匹配,需要是pointer类型是不是这样排序是对的?有高人能讲一下为什么list的排序非得搞这么一个函数干吗?
排序方法使用需要使用Pointer类型的!
使用方法举例:function CompareNames(Item1, Item2: Pointer): Integer;
begin
Result := CompareText((Item1 as TComponent).Name, (Item2 as TComponent).Name);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
List1.Sort(@CompareText);
end;排序Sort的参数是@CompareText;在你的例子里面可以FGraphicsList.Sort(@CompareFunc);