Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to sort an array of structs by multiple columns in Pascal

1 Answer

0 votes
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)

*)


 



answered Jan 29 by avibootz
...