unit sort_u;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  arrstr=array[1..20] of string;
  TForm1 = class(TForm)
    btnRead: TButton;
    btnDisplay: TButton;
    btnAtoZ: TButton;
    redtafvoer: TRichEdit;
    procedure btnReadClick(Sender: TObject);
    procedure btnDisplayClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnAtoZClick(Sender: TObject);
  private
    { Private declarations }
  public
    myfile:TextFile;
    sline:string;
    arrnames:arrstr;
    icounter:Integer;
    j:Integer;
    procedure lees(var arrnames:arrstr);
    procedure vertoon (var arrnames:arrstr);
    procedure sort (var arrnames:arrstr);
    
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.lees(var arrnames: arrstr);
begin
icounter:=0;
AssignFile(myfile,'Names.txt');
Reset(myfile);
while not Eof(myfile) do
  begin
    inc(icounter);
    Readln(myfile,arrnames[icounter]);
  end;
CloseFile(myfile);
end;

procedure TForm1.vertoon(var arrnames: arrstr);
var
  k:Integer;
begin
  for K:= 1 to icounter do
    begin
      redtafvoer.Lines.Add(arrnames[k]);
    end;
end;

procedure TForm1.btnReadClick(Sender: TObject);
begin
lees(arrnames);
end;

procedure TForm1.btnDisplayClick(Sender: TObject);
begin
vertoon(arrnames);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
redtafvoer.Lines.Clear;
end;

procedure TForm1.btnAtoZClick(Sender: TObject);
begin
redtafvoer.Lines.Clear;
sort(arrnames);
vertoon(arrnames);
end;

procedure TForm1.sort(var arrnames: arrstr);   //bubblesort
var
  k,h:Integer;
  stemp:string;
  bswap:Boolean;
begin
  repeat
  bswap:=True;
  for k := 1 to icounter-1 do
    begin
      if arrnames[k]>arrnames[k+1] then
      begin
        stemp:=arrnames[k+1];
        arrnames[k+1] := arrnames[k];
        arrnames[k] := stemp;
        bswap:=False;
      end;
    end;
   until bswap=True;
end;

end.
