Logo

Programming-Idioms

History of Idiom 28 > diff from v14 to v15

Edit summary for version 15 by :

Version 14

2015-08-23, 10:19:49

Version 15

2015-08-24, 00:10:21

Idiom #28 Sort by a property

Sort elements of array-like collection items in ascending order of x.p, where p is a field of type Item of the objects in items.

Idiom #28 Sort by a property

Sort elements of array-like collection items in ascending order of x.p, where p is a field of type Item of the objects in items.

Imports
uses fgl;
Code
type

  TItem = class p: Integer; end;
  TItems = specialize TFPGObjectList<TItem>;

var items: TItems;

  function compare(const a, b: TItem): Integer;
  begin
    Result := a.p - b.p;
  end;

begin
  items := TItems.Create;
  // Add items here.
  items.Sort(@compare);
end.  
Origin
http://wiki.freepascal.org/Generics#fgl_unit