Ver Mensaje Individual
  #27  
Antiguo 28-08-2012
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Reputación: 38
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Hola sebamawa.

Si la lista ya está ordenada podes usar una búsqueda binaria. Tomemos como ejemplo una reducción de la clase que puso de ejemplo roman:
Código Delphi [-]
implementation

type
  TCliente = class
    Nombre: String;
  end;

var
  LstObj : TObjectList;
  Cliente : TCliente;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  LstObj := TObjectList.Create;
  // Demás operaciones de carga
  ...
end;

function TForm1.BinarySearch(OLst: TObjectList; Valor: string): Integer;
var
  Pri, Ult, Med : Integer;
  Esta : boolean;
begin
   Pri  := 0;
   Ult  := OLst.Count-1;
   Esta := False;
   while (Pri <= Ult) and not Esta do
   begin
      Med := (Pri + Ult) div 2;
      if TCliente(OLst.Items[Med]).Nombre = Valor then Esta := true;
      if TCliente(OLst.Items[Med]).Nombre < Valor then Pri  := Med + 1;
      if TCliente(OLst.Items[Med]).Nombre > Valor then Ult  := Med - 1;
   end;
   if Esta then
     Result := Med
   else
     Result := -1;
end;

// Ejemplo de llamada:
procedure TForm1.Button1Click(Sender: TObject);
var
  P : Integer;
begin
  P := BinarySearch(LstObj, 'Juan Perez');
  if P <> -1 then
    ShowMessage('Nombre:   ' + TCliente(LstObj.Items[P]).Nombre+#10#13+
                'Posición: ' + IntToStr(P));
  //  ...
end;

...

procedure TForm1.FormDestroy(Sender: TObject);
begin
  if Assigned(LstObj) then
    LstObj.Free;
end;

Saludos.
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Responder Con Cita