[Lazarus] TFPGList.Sort

Andrea Mauri andrea.mauri.75 at gmail.com
Thu Oct 8 15:19:07 CEST 2015


Dear all,

I am trying to sort a simple TIntegerList defined as:

TIntegerList = specialize TFPGList<Integer>;

In order to sort I introduced a compare function as:

function CompareInt(const Item1,
   Item2: Integer): Integer;
begin
   if Item1 < Item2 then
     Result:= -1
   else if Item2 > Item1 then
     Result:= 1
   else
    Result:= 0;
end;

but I did not obtain the expected results.
Here a simple example:
procedure TForm1.Button1Click(Sender: TObject);
var
   i: Integer;
   il: TIntegerList;
begin
   il:= TIntegerList.Create;
   il.add(5);
   il.Add(3);
   il.add(8);
   il.Add(2);
   il.add(2);
   il.Add(1);
   ListBox1.Clear;
   for i:= 0 to il.Count - 1 do
     ListBox1.Items.Add(IntToStr(il[i]));
   il.Sort(@CompareInt);
   ListBox2.Clear;
   for i:= 0 to il.Count - 1 do
     ListBox2.Items.Add(IntToStr(il[i]));
   il.Free;
end;
Starting from a sequence of:
5,3,8,2,2,1
I got as sorted list:
2,2,1,3,5,8

What am I doing wrong?

Andrea




More information about the Lazarus mailing list