Ver Mensaje Individual
  #4  
Antiguo 28-03-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
ingabraham,

Cita:
Empezado por ingabraham
...tengo una lista TStringList...quiero ordenarla descendentemente...
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;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const
  A1 : Array[0..10] of Integer = (2001,2000,2005,2008,2003,2002,2006,2004,2007,2010,2009);

var
  Form1: TForm1;

implementation

{$R *.dfm}

// Ordena un TStringList por el método Sort
procedure TForm1.Button1Click(Sender: TObject);
var
   i : Integer;
   S : TStringList;


begin

   S := TStringList.Create;

   Randomize;

   for i := Low(A1) to High(A1) do
      S.Add(IntToStr(A1[i]));

   S.Sort;

   ListBox1.Sorted := False;

   ListBox1.Clear;

   for i := S.Count - 1 downto 0 do
      ListBox1.Items.Add(S[i]);

   S.Free;

end;

// Ordena un TStringList por la propiedad Sorted
procedure TForm1.Button2Click(Sender: TObject);
var
   i : Integer;
   S : TStringList;


begin

   S := TStringList.Create;

   Randomize;

   S.Sorted := True;

   for i := Low(A1) to High(A1) do
      S.Add(IntToStr(A1[i]));

   ListBox1.Sorted := False;

   ListBox1.Clear;

   for i := S.Count - 1 downto 0 do
      ListBox1.Items.Add(S[i]);

   S.Free;

end;

function CompareDesc(TS: TStrings; const i1, i2: Integer): Integer;
begin
   Result:= CompareText(TS[i2], TS[i1]);
end;

// Ordena un TStringList por el método CustomSort
procedure TForm1.Button3Click(Sender: TObject);
var
   i : Integer;
   S : TStringList;

begin

   S := TStringList.Create;

   Randomize;

   for i := Low(A1) to High(A1) do
      S.Add(IntToStr(A1[i]));

   S.CustomSort(@CompareDesc);

   ListBox1.Sorted := False;

   ListBox1.Items.Assign(S);

   S.Free;

end;

end.
El código anterior ordena un TStringList de forma descendente a través de tres métodos equivalentes y muestra el resultado en un TListBox, como se muestra en la siguiente imagen:



Espero sea útil

Nelson.

Última edición por nlsgarcia fecha: 28-03-2014 a las 15:30:09.
Responder Con Cita