Out of bouds Listbox ordenação

Delphi

27/09/2009

Fiz um Bubble Sort para ordenar um ListBox, mas está dando Out of bound, não sei porque, alguém me salva:

var
  i,j,aux,k:Integer;
begin
  k:=(Listbox1.Items.Count-1);

  for i:=1 to Listbox1.Items.Count do
  begin
    for j:=1 to Listbox1.Items.Count do
    begin
      if(strtoint(Listbox1.Items[j]) > strtoint(Listbox1.Items[j+1])) then
      begin
        aux:=strtoint(Listbox1.Items[j]);
        Listbox1.Items[j] := Listbox1.Items[j+1];
        Listbox1.Items[j+1] := inttostr(aux);
      end;
    end;
  end;


obrigado![/code]


Felipedcb

Felipedcb

Curtidas 0

Respostas

Felipedcb

Felipedcb

27/09/2009

Ainda com o mesmo erro...

  for i:=1 to Listbox1.Items.Count do
  begin
    for j:=1 to (Listbox1.Items.Count-1) do
    begin
      if(strtoint(Listbox1.Items[j]) > strtoint(Listbox1.Items[j+1])) then
      begin
        aux:=strtoint(Listbox1.Items[j]);
        Listbox1.Items[j] := Listbox1.Items[j+1];
        Listbox1.Items[j+1] := inttostr(aux);
      end;
    end;
  end; 


O delphi aponta o erro nessa linha:
      if(strtoint(Listbox1.Items[j]) > strtoint(Listbox1.Items[j+1])) then


obrigado
[/code]


GOSTEI 0
Afarias

Afarias

27/09/2009

i pode ir até Count, sendo q como a lista é baseada em ZERO, Count excede o maior índice da lista.

O mesmo para J+1


T+


GOSTEI 0
Marco Salles

Marco Salles

27/09/2009

Mas o ListBox ja tem a Propriedade Sorted

listBox1.Sorted;

ja Ordena ...


GOSTEI 0
Adilsond

Adilsond

27/09/2009

retirado do Torry´s delphi page - To sort Integer values:

...sort a Stringlist with the CustomSort Method?
Author: Thomas Stutz

function CompareInt(List: TStringList; Index1, Index2: Integer): Integer;
var
d1, d2: Integer;
r1, r2: Boolean;
function IsInt(AString : string; var AInteger : Integer): Boolean;
var
Code: Integer;
begin
Val(AString, AInteger, Code);
Result := (Code = 0);
end;
begin
r1 := IsInt(List[Index1], d1);
r2 := IsInt(List[Index2], d2);
Result := ord(r1 or r2);
if Result <> 0 then
begin
if d1 < d2 then
Result := -1
else
if d1 > d2 then
Result := 1
else
Result := 0;
end
else
Result := lstrcmp(PChar(List[Index1]), PChar(List[Index2]));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
sl: TStringList;
begin
sl := TStringList.Create;
try
// listbox1.Sorted := False;
sl.Assign(listbox1.Items);
sl.CustomSort(CompareInt);
listbox1.Items.Assign(sl);
finally
sl.Free;
end;
end;


GOSTEI 0
Marco Salles

Marco Salles

27/09/2009

é exercicio académico ???

ListBox1.sorted:=True ;


faz a mesma coisa ou estou enganado ????


GOSTEI 0
Adilsond

Adilsond

27/09/2009

ListBox1.Sorted := True;


Os itens de um listbox são string, então ao ordenar por exemplo: 1,2,10,20 ficaria: 1,10,2,20


GOSTEI 0
Marco Salles

Marco Salles

27/09/2009

É Verdade mas nada que uma boa Orientação a Objeto não resolva

Aqui um exemplo de ordenação Decrescente para Numeros >=Zero

Type
 TOrdenar=class
   fstring:String;
 end;


procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
v:real;
begin
  for I := 0 to Lbx.Count - 1 do
    begin
       v:=strtofloat(Lbx.Items.Strings[i]);
       if v > 0  then
          V:=1/V
       else
        V:=0;
       with lbx.Items do
         begin
          Objects[i]:=TOrdenar.create;
          TOrdenar(Objects[i]).fstring:=strings[i];
           v:=strtofloat(Lbx.Items.Strings[i]);
           if v > 0  then
             strings[i]:=floattostr(1/v)
           else
             strings[i]:=´aZero´;
         end;
    end;
lbx.Sorted:=true;
lbx.Sorted:=false;
 for I := 0 to Lbx.Count - 1 do
    begin
        with lbx.Items do
          begin
            strings[i]:=TOrdenar(Objects[i]).fstring;
            TOrdenar(Objects[i]).Free;
          end;
    end;
end;



GOSTEI 0
Marco Salles

Marco Salles

27/09/2009

É Verdade mas nada que uma boa Orientação a Objeto não resolva

Aqui um exemplo de ordenação Decrescente para Numeros >=Zero

Type
 TOrdenar=class
   fstring:String;
 end;


procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
v:real;
begin
  for I := 0 to Lbx.Count - 1 do
    begin
       with lbx.Items do
         begin
          Objects[i]:=TOrdenar.create;
          TOrdenar(Objects[i]).fstring:=strings[i];
           v:=strtofloat(Lbx.Items.Strings[i]);
           if v > 0  then
             strings[i]:=floattostr(1/v)
           else
             strings[i]:=´aZero´;
         end;
    end;
lbx.Sorted:=true;
lbx.Sorted:=false;
 for I := 0 to Lbx.Count - 1 do
    begin
        with lbx.Items do
          begin
            strings[i]:=TOrdenar(Objects[i]).fstring;
            TOrdenar(Objects[i]).Free;
          end;
    end;
end;



GOSTEI 0
POSTAR