Generic sequencer tcomparer

Source: Internet
Author: User

TestCodeFile:
--------------------------------------------------------------------------------
 
Unit unit1;

Interface

Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls;

Type
Tform1 = Class (tform)
Memo1: tmemo;
Memo2: tmemo;
Button1: tbutton;
Button2: tbutton;
Button3: tbutton;
Button4: tbutton;
Button5: tbutton;
Button6: tbutton;
Button7: tbutton;
Button8: tbutton;
Button9: tbutton;
Button10: tbutton;
Procedure button1click (Sender: tobject );
Procedure button2click (Sender: tobject );
Procedure button3click (Sender: tobject );
Procedure button4click (Sender: tobject );
Procedure button5click (Sender: tobject );
Procedure button6click (Sender: tobject );
Procedure button7click (Sender: tobject );
Procedure button8click (Sender: tobject );
Procedure button9click (Sender: tobject );
Procedure button10click (Sender: tobject );
End;

VaR
Form1: tform1;

Implementation

{$ R *. DFM}

Uses generics. Collections, generics. defaults; {required generic units}

// Construct a dynamic array consisting of 10 integers and then sort
Procedure tform1.button1click (Sender: tobject );
VaR
Arr: array of integer;
I: integer;
Begin
{Construct a dynamic array}
Randomize;
For I: = 0 to 9 do begin
Setlength (ARR, length (ARR) + 1 );
Arr [I]: = random (10 );
End;

{Display before sorting}
Memo1.clear;
For I: = 0 to length (ARR)-1 do memo1.lines. Add (inttostr (ARR [I]);

{Sorting}
Tarray. Sort <integer> (ARR); {It can have 1, 2, and 4 parameters. Here only one parameter is used, and others are default parameters}

{Display sorting result}
Memo2.clear;
For I: = 0 to length (ARR)-1 do memo2.lines. Add (inttostr (ARR [I]);
End;

// The second parameter of tarray. sort is of the icomparer <t> type. The default value is tcomparer <t>. default.
Procedure tform1.button2click (Sender: tobject );
VaR
Arr: array of integer;
I: integer;
Begin
Randomize;
For I: = 0 to 9 do begin
Setlength (ARR, length (ARR) + 1 );
Arr [I]: = random (10 );
End;

Memo1.clear;
For I: = 0 to length (ARR)-1 do memo1.lines. Add (inttostr (ARR [I]);

{The default value of the sorter is used for sorting, which is the same as ignoring this parameter}
Tarray. Sort <integer> (ARR, tcomparer <integer>. Default );
Memo2.clear;
For I: = 0 to length (ARR)-1 do memo2.lines. Add (inttostr (ARR [I]);
End;

// The preceding two examples have been modified.
Procedure tform1.button3click (Sender: tobject );
VaR
Arr: array of integer;
I: integer;
Comparer: icomparer <integer>;
Begin
Randomize;
For I: = 0 to 9 do begin
Setlength (ARR, length (ARR) + 1 );
Arr [I]: = random (10 );
End;

Memo1.clear;
For I: = 0 to length (ARR)-1 do memo1.lines. Add (inttostr (ARR [I]);

Comparer: = tcomparer <integer>. default;
Tarray. Sort <integer> (ARR, comparer );
Memo2.clear;
For I: = 0 to length (ARR)-1 do memo2.lines. Add (inttostr (ARR [I]);
End;

// The last two parameters of tarray. Sort can specify the sorting range:
Procedure tform1.button4click (Sender: tobject );
VaR
Arr: array of integer;
I: integer;
Comparer: icomparer <integer>;
Begin
Randomize;
For I: = 0 to 9 do begin
Setlength (ARR, length (ARR) + 1 );
Arr [I]: = random (10 );
End;

Memo1.clear;
For I: = 0 to length (ARR)-1 do memo1.lines. Add (inttostr (ARR [I]);

Comparer: = tcomparer <integer>. default;
{Here, we specify to order only three elements starting from 1st elements}
Tarray. Sort <integer> (ARR, comparer, 0, 3 );
Memo2.clear;
For I: = 0 to length (ARR)-1 do memo2.lines. Add (inttostr (ARR [I]);
End;

// If You Want To sort data in reverse order, you can create your own sequencer. The following describes how to build and implement a tmycomparer and then call it:
Type
Tmycomparer = Class (tcomparer <integer>)
Public
Function compare (const left, right: integer): integer; override;
End;

{Tmycomparer}
Function tmycomparer. Compare (const left, right: integer): integer;
Begin
Result: = right-left;
End;

Procedure tform1.button5click (Sender: tobject );
VaR
Arr: array of integer;
I: integer;
Comparer: tmycomparer;
Begin
Randomize;
For I: = 0 to 9 do begin
Setlength (ARR, length (ARR) + 1 );
Arr [I]: = random (10 );
End;

Memo1.clear;
For I: = 0 to length (ARR)-1 do memo1.lines. Add (inttostr (ARR [I]);

Comparer: = tmycomparer. Create;
Tarray. Sort <integer> (ARR, comparer );
Memo2.clear;
For I: = 0 to length (ARR)-1 do memo2.lines. Add (inttostr (ARR [I]);
Comparer. Free;
End;

// You can also use the tcomparer <t>. Construct method to construct a sequencer using a function in the tcomparison format.
Function myfunc1 (const left, right: integer): integer;
Begin
Result: = right-left;
End;

Procedure tform1.button6click (Sender: tobject );
VaR
Arr: array of integer;
I: integer;
Comparer: icomparer <integer>;
Begin
Randomize;
For I: = 0 to 9 do begin
Setlength (ARR, length (ARR) + 1 );
Arr [I]: = random (10 );
End;

Memo1.clear;
For I: = 0 to length (ARR)-1 do memo1.lines. Add (inttostr (ARR [I]);

Comparer: = tcomparer <integer>. Construct (myfunc1 );
Tarray. Sort <integer> (ARR, comparer );
Memo2.clear;
For I: = 0 to length (ARR)-1 do memo2.lines. Add (inttostr (ARR [I]);
End;

// If You Want To sort elements of the custom type, you can only use the self-built sorter.
Type
Tperson = record
Name: string;
Age: word;
End;

Function myfunc2 (const left, right: tperson): integer;
Begin
Result: = left. Age-Right. Age;
End;

Procedure tform1.button7click (Sender: tobject );
VaR
Arr: array of tperson;
I: integer;
Comparer: icomparer <tperson>;
Begin
Setlength (ARR, 4 );
Arr [0]. Name: = 'a'; arr [0]. Age: = 22;
Arr [1]. Name: = 'bb'; arr [1]. Age: = 33;
Arr [2]. Name: = 'cc'; arr [2]. Age: = 44;
Arr [3]. Name: = 'dd'; arr [3]. Age: = 11;

Memo1.clear;
For I: = 0 to length (ARR)-1 do
Memo1.lines. Add (format ('% s: % d', [arr [I]. Name, arr [I]. Age]);

Comparer: = tcomparer <tperson>. Construct (myfunc2 );
Tarray. Sort <tperson> (ARR, comparer );
Memo2.clear;
For I: = 0 to length (ARR)-1 do
Memo2.lines. Add (format ('% s: % d', [arr [I]. Name, arr [I]. Age]);
End;

// Tstringcomparer. ordinal is an officially implemented string sequencer and can be directly used.
// But it seems that there is a problem (Delphi 2010-14.0.3513.24210), and later versions should be able to be changed.
Procedure tform1.button8click (Sender: tobject );
VaR
Arr: array of string;
I: integer;
Begin
Setlength (ARR, 4 );
Arr [0]: = '000000 ';
Arr [1]: = '000000 ';
Arr [2]: = 'bbb ';
Arr [3]: = 'aaa ';

Memo1.clear;
For I: = 0 to length (ARR)-1 do memo1.lines. Add (ARR [I]);

Tarray. Sort <string> (ARR, tstringcomparer. ordinal );
Memo2.clear;
For I: = 0 to length (ARR)-1 do memo2.lines. Add (ARR [I]);
End;

// The following describes how to sort string arrays.
Function mycomparerfunc (const S1, S2: string): integer;
Begin
Result: = comparetext (S2, S1 );
// Result: =-comparetext (S1, S2); {or so}
End;

Procedure tform1.button9click (Sender: tobject );
VaR
Arr: array of string;
I: integer;
Comparer: icomparer <string>;
Begin
Memo1.clear;
Memo1.lines. commatext: = '11, 33,22, AAA, CCC, BBB ';
Setlength (ARR, memo1.lines. Count );
For I: = 0 to length (ARR)-1 do arr [I]: = memo1.lines [I];

Comparer: = tcomparer <string>. Construct (mycomparerfunc );
Tarray. Sort <string> (ARR, comparer );

Memo2.clear;
For I: = 0 to length (ARR)-1 do memo2.lines. Add (ARR [I]);
End;

// You can set the previousProgramAbbreviated:
Procedure tform1.button10click (Sender: tobject );
VaR
Arr: array of string;
I: integer;
Comparer: icomparer <string>;
Begin
Memo1.clear;
Memo1.lines. commatext: = '11, 33,22, AAA, CCC, BBB ';
Setlength (ARR, memo1.lines. Count );
For I: = 0 to length (ARR)-1 do arr [I]: = memo1.lines [I];

Comparer: = tcomparer <string>. Construct (
Function (const S1, S2: string): integer
Begin
Result: = comparetext (S2, S1 );
End );
Tarray. Sort <string> (ARR, comparer );

Memo2.clear;
For I: = 0 to length (ARR)-1 do memo2.lines. Add (ARR [I]);
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.