uses crt;
const nmax=100;
      line='-------------------------------------------------------------------------------';
      shap='|       Фамилия                                                | Отдел | Стаж |';
type Sotrudniki = record
       Surname,SecondName,Name:string[20];
       Otdel, Stazh: byte;
  end;
  var p: Sotrudniki;
      a: array [1..nmax] of Sotrudniki;
      n,i,k,re:integer;
      f:file of Sotrudniki;
      ss: real;
      x: string;
  begin
  clrscr;
  assign(f,'C:\Sotrudniki.dat');
  rewrite(f);
  write(' Введите количество сотрудников предприятия :  ');
  readln(n);
  for i:=1 to n do
      with p do
           begin
           writeln;
           writeln(' Сотрудник № ',i);
             write(' Фамилия : '); readln(SecondName);
             write(' Имя : '); readln(Name);
             write(' Отчество :  '); readln(Surname);
             writeln;
             write(' Номер отдела :  '); readln(Otdel);
             write(' Стаж работы : '); readln(Stazh);
             write(f,p);
           end;
writeln('файл записан.Для продолжения работы нажмите "Enter" ');
readln;
reset(f);
n:=0;
while not (eof(f)) do
      begin
      read(f,p);
      inc(n);
      a[n]:=p;
      end;
re:=0;
while re<=n*2 do
      begin
      for i:=1 to n-1 do
          if (a[i].Otdel=a[i+1].Otdel) and (a[i].Stazh<a[i+1].Stazh) then
             begin
             p:=a[i];
             a[i]:=a[i+1];
             a[i+1]:=p;
             end else
                 if a[i].Otdel>a[i+1].Otdel then
                    begin
                    p:=a[i];
                    a[i]:=a[i+1];
                    a[i+1]:=p;
                    end;
      inc(re);
      end;
close(f);
rewrite(f);
for i:=1 to n do
    with a[i] do
         write(f,a[i]);
clrscr;
writeln(' Список сотрудников по отделам ');
writeln(line);
writeln(shap);
writeln(line);
reset(f);
while not (eof(f)) do
      begin
      read(f,p);
      with p do
           writeln('|',Secondname:20,'',Name:20,'',Surname:20,'|':2,Otdel:4,'|':4,Stazh:4,'|':3);
      writeln(line);
      end;
close(f);
readln;
end.