Che cosa ho sbagliato in questo codice:

codice:
procedure TForm5.SortTabella;
var can:boolean;
  I,temp: Integer;
  appo: Tree;
begin
temp := 0;
repeat
for I := 0 to 256 - 1 do
  begin
    can:=true;

  if ArrayList[i]<> nil then
    if ArrayList[temp]=nil then
      temp := i
    else
      if ArrayList[i].Freq < ArrayList[temp].Freq then
        begin
        appo := ArrayList[i];
        ArrayList[i] := ArrayList[temp];
        ArrayList[temp] := appo;

        temp := i;
        can:=false;
        end;
  end;
until (can=true);
end;
arraylist è un array 0..255 di valori Tree.
Tree è una classe definita così:
codice:
type
  PTree = ^Tree;
  Tree = class
    Code: byte;
    Freq: integer;
    Left: PTree;
    Right: PTree;
    constructor Create(aCode: byte; aFreq: Integer; aLeft: PTree; aRight: PTree);
  end;
durante l'inizializzazione dell'array solo alcuni valori vengono inizializzati, gli altri rimangono nil. Io vorrei ordinare i valori non nil in ordine crescente, ma il codice non funziona... perchè??

grazie ciao