Sign in Join

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

Mailing lists / lazarus / 2016-05

Example of TObjectList sorting

10 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Richard Mace

Hi All,

I have a TObjectList that contains many objects that have a property called "Position" which is an integer.

Could somebody show me an example of the code that I will need to create that will sort all of my Objects within the TObjectList so that they are in position order e.g. 1,2,3,4,5 and so on?

Thanks

Richard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20160509/8b69edf4/attachment.html>

Reply by email

Denis Kozlov
to Richard Mace
Richard Mace wrote 7 lines
I have a TObjectList that contains many objects that have a property called "Position" which is an integer. Could somebody show me an example of the code that I will need to create that will sort all of my Objects within the TObjectList so that they are in position order e.g. 1,2,3,4,5 and so on?

Call TList.Sort method and supply a comparison function.

For example:

List.Sort(@CompareByPositionPtr);
function CompareByPosition(A, B: TMyObject): Integer; inline;
begin
  if A.Position < B.Position then
    Result := -1
  else if A.Position > B.Position then
    Result := 1
  else
    Result := 0;
end;
function CompareByPositionPtr(A, B: Pointer): Integer;
begin
  Result := CompareByPosition(TMyObject(A), TMyObject(B));
end;

This kind of question is better suited on the forum I think

Denis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20160509/4fc4db14/attachment.html>

Reply by email

Graeme Geldenhuys
to Denis Kozlov
Denis Kozlov wrote
This kind of question is better suited on the forum I think

Why? This mailing list is just as much a support area as the web forum. Not everybody likes web based forums (I don't).

Granted the question related more to the RTL that Lazarus or LCL, so if anything, it could have been posted in the fpc-users mailing list. But you were kind enough to answer anyway. ;-)

Regards,
Graeme

Reply by email

Denis Kozlov
to Graeme Geldenhuys
Graeme Geldenhuys wrote 6 lines
On 2016-05-09 13:24, Denis Kozlov wrote: > This kind of question is better suited on the forum I think Why? This mailing list is just as much a support area as the web forum. Not everybody likes web based forums (I don't).

Forum is especially good for tutorials, generic and commonly asked questions - like a repository of knowledge, Also, forums are more easily searched, indexed by search engines and easier to view for novice users. Hence, I think this question/answer would reach more end users while being on the forum, instead of on a mailing list.

Denis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20160509/a14d1cfa/attachment.html>

Reply by email

Graeme Geldenhuys
to Denis Kozlov
Denis Kozlov wrote
Also, forums are more easily searched, indexed by search engines and easier to view for novice users.

Lets agree to disagree. ;-)

ps:
The mailing lists are also searched and indexed by search engines - thanks to services like Nabble and many others. But then again, my email client does just fine searching on its own (and can do it offline).

http://free-pascal-lazarus.989080.n3.nabble.com/
http://free-pascal-general.1045716.n5.nabble.com/

Regards,
Graeme

Reply by email

Richard Mace
to Denis Kozlov: Call TList.Sort method and supply a comparison function. For example:…

Hi Denis,

Thanks for your reply.

Denis Kozlov wrote 17 lines
On 9 May 2016 at 12:48, Richard Mace wrote: > I have a TObjectList that contains many objects that have a property > called "Position" which is an integer. > > Could somebody show me an example of the code that I will need to create > that will sort all of my Objects within the TObjectList so that they are in > position order e.g. 1,2,3,4,5 and so on? > Call TList.Sort method and supply a comparison function. For example: List.Sort(@CompareByPositionPtr);

?Would I call the List.sort externally, or would the List call sort when an Add has happened, as I don't seem to have a ".sort" method on my TObjectList?

Thanks?

?Richard?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20160521/1187da0a/attachment.html>

Reply by email

Denis Kozlov
to Richard Mace
Richard Mace wrote
?Would I call the List.sort externally, or would the List call sort when an Add has happened, as I don't seem to have a ".sort" method on my TObjectList?

You need to call List.Sort yourself, it is not sorted automatically when you add more elements.

TObjectList does have a Sort method, since it descends from TList class.

Denis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20160522/36121bad/attachment.html>

Reply by email

Richard Mace
to Denis Kozlov

Thanks Denis,

I am now getting:

inboundroutedestlist.pas(49,35) Error: Incompatible type for arg no. 1: Got "<procedure variable type of function(Pointer;Pointer):LongInt of object;Register>", expected "<procedure variable type of function(Pointer;Pointer):LongInt;Register>"

Any ideas?

Richard

Denis Kozlov wrote 15 lines
On 21 May 2016 at 13:49, Richard Mace wrote: > ?Would I call the List.sort externally, or would the List call sort when > an Add has happened, as I don't seem to have a ".sort" method on my > TObjectList? > You need to call List.Sort yourself, it is not sorted automatically when you add more elements. TObjectList does have a Sort method, since it descends from TList class. Denis

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

Reply by email

Ondrej Pokorny
to Richard Mace

You are using "procedure of object" where you need "procedure".

(One solution is to add static keyword to your procedure declaration, if the FPC version you use supports the typecast - iirc, fpc 3.0.0 can't do that in objfpc mode.)

Ondrej

Richard Mace wrote 40 lines
Thanks Denis, I am now getting: inboundroutedestlist.pas(49,35) Error: Incompatible type for arg no. 1: Got "<procedure variable type of function(Pointer;Pointer):LongInt of object;Register>", expected "<procedure variable type of function(Pointer;Pointer):LongInt;Register>" Any ideas? Richard On 22 May 2016 at 07:30, Denis Kozlov < <mailto:>> wrote: On 21 May 2016 at 13:49, Richard Mace < <mailto:>> wrote: ?Would I call the List.sort externally, or would the List call sort when an Add has happened, as I don't seem to have a ".sort" method on my TObjectList? You need to call List.Sort yourself, it is not sorted automatically when you add more elements. TObjectList does have a Sort method, since it descends from TList class. Denis -- _______________________________________________ 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/20160522/2e7dea72/attachment.html>

Reply by email