简体   繁体   中英

How to add data from an array into a TRichEdit from smallest to largest in Delphi

I want to read names and corresponding values and I will store them in an array. I was wondering how I can read through the array, add the highest number to the TRichEdit, and then keep reading through the array and add the second highest, third highest and so on until there are no more values.

There are three main steps to solve your problem.

  1. You have to be the unsorted array (create one)
  2. Sort the array
  3. Add the sorted items to the TRichEdit

The pas file:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    RichEdit1: TRichEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
    Generics.Defaults
  , Generics.Collections
  ;

{$R *.dfm}

type
  TMyRec = packed record
    int : integer;
    str : string;
    constructor create( int_ : integer; str_ : string );
  end;

constructor TMyRec.create( int_ : integer; str_ : string );
begin
  int := int_;
  str := str_;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  myRecs : array of TMyRec;
  myRecComparer : IComparer<TMyRec>;
  myRec : TMyRec;
begin
  setLength( myRecs, 5 );
  try
    // Create and fill up the unsorted array
    myRecs[0] := TMyRec.create( 3, '3' );
    myRecs[1] := TMyRec.create( 5, '5' );
    myRecs[2] := TMyRec.create( 1, '1' );
    myRecs[3] := TMyRec.create( 4, '4' );
    myRecs[4] := TMyRec.create( 2, '2' );

    // Sort the array
    myRecComparer := TComparer<TMyRec>.Construct( function ( const left_, right_ : TMyRec ) : integer begin result := left_.int - right_.int end );
    TArray.sort<TMyRec>( myRecs, myRecComparer );

    // Add the sorted array items to the TRichEdit control
    RichEdit1.lines.clear;
    for myRec in myRecs do
      RichEdit1.Lines.Add( myRec.str );

  finally
    setLength( myRecs, 0 );
  end;
end;

end.

The dfm file:

object Form1: TForm1
  Left = 400
  Top = 219
  Caption = 'Form1'
  ClientHeight = 138
  ClientWidth = 200
  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 = 8
    Top = 103
    Width = 185
    Height = 25
    Caption = 'Add orders records'
    TabOrder = 0
    OnClick = Button1Click
  end
  object RichEdit1: TRichEdit
    Left = 8
    Top = 8
    Width = 185
    Height = 89
    Font.Charset = EASTEUROPE_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 1
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM