Skip to content

Commit e832abf

Browse files
Add files via upload
1 parent 31ec4d1 commit e832abf

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

example6.lpr

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
program example6;
2+
3+
{$mode objfpc}{$H+}
4+
5+
uses
6+
{$IFDEF UNIX}
7+
cthreads,
8+
{$ENDIF}
9+
Classes, SysUtils, CustApp
10+
{ you can add units after this }
11+
, fgl, uPerson;
12+
13+
type
14+
TPersonList = specialize TFPGList<TPerson>;
15+
16+
var
17+
LPersonList: TPersonList;
18+
LPersonInterator: specialize TFPGListEnumerator<TPerson>;
19+
LPerson: TPerson;
20+
21+
begin
22+
LPersonList := TPersonList.Create;
23+
LPersonList.Add(TPerson.Create('Malcolm', 30));
24+
LPersonList.Add(TPerson.Create('Bob', 19));
25+
LPersonList.Add(TPerson.Create('Alice', 30));
26+
LPersonList.Add(TPerson.Create('Charlie', 30));
27+
28+
LPersonList.Sort(@ComparePerson);
29+
30+
LPersonInterator := LPersonList.GetEnumerator;
31+
while LPersonInterator.MoveNext do
32+
begin
33+
LPerson := LPersonInterator.Current;
34+
Writeln('Person Name: ', LPerson.Name_, ', Age: ', LPerson.Age);
35+
end;
36+
37+
LPersonList.Free;
38+
Readln;
39+
end.
40+

uperson.pas

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
unit uPerson;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
uses
8+
Classes, SysUtils;
9+
10+
type
11+
{ TPerson }
12+
13+
TPerson = class
14+
private
15+
FAge: integer;
16+
FName_: string;
17+
procedure SetAge(AValue: integer);
18+
procedure SetName_(AValue: string);
19+
public
20+
constructor Create(AName: string; AAge: integer);
21+
property Name_: string read FName_ write SetName_;
22+
property Age: integer read FAge write SetAge;
23+
end;
24+
25+
function ComparePerson(const Item1, Item2: TPerson): Integer;
26+
27+
implementation
28+
29+
{ TPerson }
30+
31+
function ComparePerson(const Item1, Item2: TPerson): Integer;
32+
begin
33+
Result := AnsiCompareStr(Item1.Name_,Item2.Name_);
34+
end;
35+
36+
procedure TPerson.SetAge(AValue: integer);
37+
begin
38+
if FAge=AValue then Exit;
39+
FAge:=AValue;
40+
end;
41+
42+
procedure TPerson.SetName_(AValue: string);
43+
begin
44+
if FName_=AValue then Exit;
45+
FName_:=AValue;
46+
end;
47+
48+
constructor TPerson.Create(AName: string; AAge: integer);
49+
begin
50+
FName_:= AName;
51+
FAge:=AAge;
52+
end;
53+
54+
end.
55+

0 commit comments

Comments
 (0)