program SortItems;
type
Item = record
A : Integer;
B : Integer;
LabelStr : string;
end;
ItemArray = array of Item;
var
Data : ItemArray;
{---------------------------------------------------------------}
procedure MakeData(var Arr : ItemArray);
begin
SetLength(Arr, 7);
Arr[0].A := 7; Arr[0].B := 2; Arr[0].LabelStr := 'python';
Arr[1].A := 8; Arr[1].B := 3; Arr[1].LabelStr := 'c';
Arr[2].A := 3; Arr[2].B := 5; Arr[2].LabelStr := 'c++';
Arr[3].A := 4; Arr[3].B := 1; Arr[3].LabelStr := 'c#';
Arr[4].A := 3; Arr[4].B := 2; Arr[4].LabelStr := 'java';
Arr[5].A := 7; Arr[5].B := 1; Arr[5].LabelStr := 'go';
Arr[6].A := 1; Arr[6].B := 2; Arr[6].LabelStr := 'rust';
end;
{---------------------------------------------------------------}
procedure PrintItem(const X : Item);
begin
WriteLn('(', X.A, ', ', X.B, ', ', X.LabelStr, ')');
end;
{---------------------------------------------------------------}
procedure PrintData(const Arr : ItemArray);
var
I : Integer;
begin
for I := 0 to High(Arr) do
PrintItem(Arr[I]);
end;
{---------------------------------------------------------------}
procedure SortData(var Arr : ItemArray);
var
I, J : Integer;
Temp : Item;
begin
for I := 0 to High(Arr) do
for J := I + 1 to High(Arr) do
begin
if (Arr[I].A > Arr[J].A) or
((Arr[I].A = Arr[J].A) and (Arr[I].B > Arr[J].B)) then
begin
Temp := Arr[I];
Arr[I] := Arr[J];
Arr[J] := Temp;
end;
end;
end;
{---------------------------------------------------------------}
function FindByLabel(const Arr : ItemArray; S : string) : Integer;
var
I : Integer;
begin
FindByLabel := -1;
for I := 0 to High(Arr) do
if Arr[I].LabelStr = S then
begin
FindByLabel := I;
Exit;
end;
end;
{---------------------------------------------------------------}
procedure FilterByA(const Arr : ItemArray; Value : Integer);
var
I : Integer;
begin
for I := 0 to High(Arr) do
if Arr[I].A = Value then
PrintItem(Arr[I]);
end;
{---------------------------------------------------------------}
begin
MakeData(Data);
SortData(Data);
WriteLn('Sorted data:');
PrintData(Data);
WriteLn;
WriteLn('Searching for "java":');
if FindByLabel(Data, 'java') <> -1 then
PrintItem(Data[FindByLabel(Data, 'java')]);
WriteLn;
WriteLn('Filtering items where A = 7:');
FilterByA(Data, 7);
end.
(*
run:
Sorted data:
(1, 2, rust)
(3, 2, java)
(3, 5, c++)
(4, 1, c#)
(7, 1, go)
(7, 2, python)
(8, 3, c)
Searching for "java":
(3, 2, java)
Filtering items where A = 7:
(7, 1, go)
(7, 2, python)
*)