7. Use the generic tarray to sort Dynamic Arrays

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.