Ver Mensaje Individual
  #6  
Antiguo 21-04-2014
Avatar de nlsgarcia
[nlsgarcia] nlsgarcia is offline
Miembro Premium
 
Registrado: feb 2007
Ubicación: Caracas, Venezuela
Posts: 2.206
Reputación: 21
nlsgarcia Tiene un aura espectacularnlsgarcia Tiene un aura espectacular
ZayDun,

Cita:
Empezado por ZayDun
...lo que quiero hacer es ordenar un ListBox que contiene datos de este tipo (Código y Usuario)...
Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    ListBox2: TListBox;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure SortListBox(InputListBox, OutPutListBox: TStrings);

  function Sort(Input: TStrings; const i1, i2: Integer): Integer;
  begin
    Result:= CompareText(Copy(Input[i1],1,3) + Copy(Input[i1],4,MaxInt),
                         Copy(Input[i2],1,3) + Copy(Input[i2],4,MaxInt));
  end;

var
  SL : TStringList;

begin
   SL := TStringList.Create;
   try
      SL.Assign(InputListBox);
      SL.CustomSort(@Sort);
      OutPutListBox.Assign(SL);
   finally
      SL.Free;
   end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   SortListBox(ListBox1.Items,ListBox2.Items);
end;

end.
El código anterior, ordena un TListBox que contiene dos campos (Código y Usuario) de menor a mayor por medio de un CustomSort, como se muestra en la siguiente imagen:



El código sugerido funciona correctamente según lo esperado en Delphi 7, Delphi 2010 y Delphi XE4 bajo Windows 7 Professional x32.

Espero sea útil

Nelson.

Última edición por nlsgarcia fecha: 21-04-2014 a las 21:20:41.
Responder Con Cita