Ver Mensaje Individual
  #6  
Antiguo 29-03-2004
Avatar de roman
roman roman is offline
Moderador
 
Registrado: may 2003
Ubicación: Ciudad de México
Posts: 20.269
Reputación: 10
roman Es un diamante en brutoroman Es un diamante en brutoroman Es un diamante en bruto
Es increíble pero realmente no parece haber manera de hacerlo con el mismo ListBox no quedando más remedio que utilizar algo externo como indica marto. Para facilitar un poco las cosas y no tener que implementar uno mismo el algoritmo de ordenación puede usarse esta técnica que me encontré en una búsqueda por la red:

1. Asignar los elementos de ListBox1.Items a un StringList
2. Usar el método CustomSort del StringList
3. Devolver los elementos al ListBox

Quedaría algo así:

Código:
function CompareNumbers(List: TStringList; Index1, Index2: Integer): Integer;
var
  Number1, Number2: Integer;

begin
  Number1 := StrToInt(List[Index1]);
  Number2 := StrToInt(List[Index2]);

  if Number1 < Number2 then
    Result := -1
  else if Number1 = Number2 then
    Result := 0
  else
    Result := 1;
end;

procedure SortList(ListBox: TListBox);
var
  List: TStringList;

begin
  List := TStringList.Create;

  try
    List.AddStrings(ListBox.Items);
    List.CustomSort(CompareNumbers);

    ListBox.Clear;
    ListBox.Items.AddStrings(List);
  finally
    List.Free;
  end;
end;
// Saludos
Responder Con Cita