function comp(var p,q : log_type): boolean;
var test : boolean;
begin
  test := false;
  case comparison of
    0  : if ((p.suffix = q.suffix) AND ((p.prefix+p.area) < (q.prefix+q.area)))
            OR (p.suffix < q.suffix)
         then test := true;
    1  : if ( (p.area < q.area) OR
            ( (p.area = q.area) AND (p.suffix < q.suffix) ) OR
            ( (p.area = q.area) AND (p.suffix = q.suffix)
              AND (p.prefix < q.prefix) ) )
           THEN test := true;
    end;
  comp := test;
end;

procedure qsort(var x: net_array; m,n : integer);
var  i,j : integer;

  procedure partit(var a: net_array; var i,j : integer;
                   left, right: integer);
  var pivot: log_type;

    procedure swap (var p,q : log_type);
    var  hold : log_type;
    begin
      hold := p;
      p := q;
      q := hold;
    end;

  begin
    pivot := a[(left+right) div 2]^;
    i := left;
    j := right;
    while ( i <= j) do
    begin
      while (comp(a[i]^, pivot)) do
      i := i + 1;
      while (comp(pivot, a[j]^)) do
        j := j - 1;
        if (i <= j ) then
        begin
          swap(a[i]^,a[j]^);
          i := i + 1;
          j := j - 1;
        end;
    end;
  end;

begin { qsort }
  if (m < n) then
  begin
    partit(x,i,j,m,n);
    qsort(x,m,j);
    qsort(x,i,n);
  end;
end;

procedure sort(var x: net_array; n : integer);
begin
  comparison := 0;
  write('Sorting by suffix, prefix, area');
  qsort(x,1,n);
end;

procedure re_sort(var x: net_array; n : integer);
begin
  comparison := 1;
  write('Sorting by area, suffix, prefix');
  qsort(x,1,n);
end;

