Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2015-10

TFPGList.Sort

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Andrea Mauri

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

Reply by email

Michael Van Canneyt
to Andrea Mauri
Andrea Mauri wrote 14 lines
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

This is wrong. It is in fact the same condition as your first if. it should be

   if Item1>Item2 then

Michael.

Reply by email

Maciej Izak
to Andrea Mauri: I am trying to sort a simple TIntegerList defined as: TIntegerList = specialize…

As alternative you can use https://github.com/dathox/generics.collections (requires FPC 3.0).

You can use TList from Generics.Collections, is much simpler in usage:

======code begin======
type
  TIntegerList = specialize TList<Integer>;
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();
  ListBox2.Clear;
  for i:= 0 to il.Count - 1 do
    ListBox2.Items.Add(IntToStr(il[i]));
  il.Free;
end;
======code end======
Best regards, Maciej Izak -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151008/c1e1eb01/attachment.html>

Reply by email

Andrea Mauri
to Michael Van Canneyt: This is wrong. It is in fact the same condition as your first if. it should be if…

Thanks and sorry for this stupid mistake.
Il 08/ott/2015 16:47, "Michael Van Canneyt" ha
scritto:

Show quoted text 32 lines
On Thu, 8 Oct 2015, Andrea Mauri wrote: 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 > This is wrong. It is in fact the same condition as your first if. it should be if Item1>Item2 then Michael. -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151008/2874d5b4/attachment.html>

Reply by email