[Lazarus] Example of TObjectList sorting
Denis Kozlov
dezlov at gmail.com
Mon May 9 14:24:30 CEST 2016
On 9 May 2016 at 12:48, Richard Mace <richard.mace at gmail.com> 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);
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-ide.org/pipermail/lazarus/attachments/20160509/4fc4db14/attachment-0003.html>
More information about the Lazarus
mailing list