PAS
Unit unit1;
Interface
Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls;
Type
Tform1 = Class (tform)
Button1: tbutton;
Button2: tbutton;
Memo1: tmemo;
Procedure button1click (Sender: tobject );
Procedure button2click (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;
VaR
Form1: tform1;
Implementation
{$ R *. DFM}
Uses
Generics. collections; {reference a generic unit}
{Sort string arrays}
Procedure tform1.button1click (Sender: tobject );
VaR
Arr: array of string;
I: integer;
Begin
{Set the dynamic array size}
Setlength (ARR, memo1.lines. Count );
{Copy text from memo1 to an array}
For I: = 0 to memo1.lines. Count-1 do arr [I]: = memo1.lines [I];
Tarray. Sort <string> (ARR );
{View data in the array in memo1}
Memo1.clear;
For I: = 0 to length (ARR)-1 do memo1.lines. Add (ARR [I]);
End;
{Sort by integer array}
Procedure tform1.button2click (Sender: tobject );
VaR
Arr: array of integer;
I: integer;
Const
Num = 10;
Begin
Setlength (ARR, num );
Randomize;
Memo1.clear;
// Generate 10 random numbers into the array and display them in memo
For I: = 0 to num-1 do begin
Arr [I]: = random (100 );
Memo1.lines. Add (inttostr (ARR [I]);
End;
// Sort
Tarray. Sort <integer> (ARR );
// Wait for one second
Sleep (1000 );
Memo1.clear;
For I: = 0 to num-1 do begin
Memo1.lines. Add (inttostr (ARR [I]);
End;
End;
End.
DFM
Object form1: tform1
Left = 0
Top = 0
Caption = 'form1'
Clientheight = 1, 217
Clientwidth = 290
Color = clbtnface
Font. charset = default_charset
Font. Color = clwindowtext
Font. Height =-11
Font. Name = 'tahoma'
Font. Style = []
Oldcreateorder = false
Pixelsperinch = 96
Textheight = 13
Object button1: tbutton
Left = 1, 207
Maximum = 152
Width = 75
Height = 25
Caption = 'button1'
Taborder = 0
Onclick = button1click
End
Object button2: tbutton
Left = 1, 207
Maximum = 183
Width = 75
Height = 25
Caption = 'button2'
Taborder = 1
Onclick = button2click
End
Object memo1: tmemo
Left = 8
Top = 8
Width = 193
Height = 200
Lines. Strings = (
'Mo1 ')
Taborder = 2
End
End