Generic sequencer tcomparer

Source: Internet
Author: User
Test code file:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) memo1: tmemo; memo2: tmemo; example: tbutton; button2: tbutton; button3: tbutton; button4: tbutton; button5: tbutton; button6: example; example: tbutton; button9: tbutton; button10: tbutton; procedure extract (Sender: tobject); Procedure button2click (Sender: tobject); Procedure extract (Sender: tobject ); procedure alert (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 {build 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]); {sort} 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; // tarray. the second parameter of sort is the icomparer <t> type. The default value is tcomparer <t>. defaultprocedure 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]); {use the default value of the sorter 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 same as the preceding example. You can use 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; // tarray. the following two parameters of 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; {starting from 1st elements, only three elements are sorted here} 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 sorters. 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 tcomparer <t>. the construct method uses a function in the tcomparison format to construct the sequencer. In this way, some simple functions 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]: = '2016 '; 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 is the sort 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 abbreviated the previous program as 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.
 

Test the Form file (the reason why the Form file is attached is to facilitate the test again ):

object Form1: TForm1  Left = 0  Top = 0  Caption = 'Form1'  ClientHeight = 321  ClientWidth = 318  Color = clBtnFace  Font.Charset = DEFAULT_CHARSET  Font.Color = clWindowText  Font.Height = -11  Font.Name = 'Tahoma'  Font.Style = []  OldCreateOrder = False  PixelsPerInch = 96  TextHeight = 13  object Memo1: TMemo    Left = 0    Top = 0    Width = 89    Height = 321    Align = alLeft    Lines.Strings = (      'Memo1')    TabOrder = 0    ExplicitHeight = 299  end  object Button1: TButton    Left = 120    Top = 8    Width = 75    Height = 25    Caption = 'Button1'    TabOrder = 1    OnClick = Button1Click  end  object Button2: TButton    Left = 120    Top = 39    Width = 75    Height = 25    Caption = 'Button2'    TabOrder = 2    OnClick = Button2Click  end  object Button3: TButton    Left = 120    Top = 70    Width = 75    Height = 25    Caption = 'Button3'    TabOrder = 3    OnClick = Button3Click  end  object Memo2: TMemo    Left = 219    Top = 0    Width = 99    Height = 321    Align = alRight    Lines.Strings = (      'Memo2')    TabOrder = 4    ExplicitHeight = 299  end  object Button4: TButton    Left = 120    Top = 101    Width = 75    Height = 25    Caption = 'Button4'    TabOrder = 5    OnClick = Button4Click  end  object Button5: TButton    Left = 120    Top = 132    Width = 75    Height = 25    Caption = 'Button5'    TabOrder = 6    OnClick = Button5Click  end  object Button6: TButton    Left = 120    Top = 163    Width = 75    Height = 25    Caption = 'Button6'    TabOrder = 7    OnClick = Button6Click  end  object Button7: TButton    Left = 120    Top = 194    Width = 75    Height = 25    Caption = 'Button7'    TabOrder = 8    OnClick = Button7Click  end  object Button8: TButton    Left = 120    Top = 225    Width = 75    Height = 25    Caption = 'Button8'    TabOrder = 9    OnClick = Button8Click  end  object Button9: TButton    Left = 120    Top = 256    Width = 75    Height = 25    Caption = 'Button9'    TabOrder = 10    OnClick = Button9Click  end  object Button10: TButton    Left = 120    Top = 287    Width = 75    Height = 25    Caption = 'Button10'    TabOrder = 11    OnClick = Button10Click  endend
 

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.