Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#8

AW: Sortierfunktion nach Datum in einer TStringList

  Alt 4. Jul 2015, 00:42
Das ist eigentlich alles was du brauchst:
Delphi-Quellcode:
unit MyObjectList;

interface

uses
  {System.}Classes,
  {System.}Generics.Collections,
  {System.}SysUtils;

type
  TMyObjectList<T: class> = class( TObjectList<T> )
  public
    procedure ToStrings( AStrings: TStrings; IncludeObjects: Boolean = false ); overload;
    procedure ToStrings( AStrings: TStrings; AConverter: TFunc<T, string>; IncludeObjects: Boolean = false ); overload;
  end;

implementation

{ TMyObjectList<T> }

procedure TMyObjectList<T>.ToStrings( AStrings: TStrings; IncludeObjects: Boolean );
begin
  ToStrings( AStrings,
    function( AItem: T ): string
    begin
      Result := AItem.ToString( );
    end, IncludeObjects );
end;

procedure TMyObjectList<T>.ToStrings( AStrings: TStrings; AConverter: TFunc<T, string>; IncludeObjects: Boolean );
var
  LItem: T;
begin
  AStrings.BeginUpdate;
  try
    AStrings.Clear;
    for LItem in Self do
    begin
      if IncludeObjects then
        AStrings.AddObject( AConverter( LItem ), LItem )
      else
        AStrings.Add( AConverter( LItem ) );
    end;
  finally
    AStrings.EndUpdate;
  end;
end;

end.
Da sammelst du alle Instanzen, sortierst diese, wie gewünscht und lässt dir mit den beiden Methoden die StringListen befüllen:
Delphi-Quellcode:
FFileInfos : TMyObjectList<TFileInfo>;

FFileInfos.Sort( {Sortier-Methode} ); // Sortierung für ListBox
FFileInfos.ToStrings(
  ListBox1.Items,
  True );

FFileInfos.Sort( {Sortier-Methode} ); // Sortierung für ComboBox
FFileInfos.ToStrings(
  ComboBox1.Items,
  function ( Item: TFileInfo ) : string
  begin
    Result := Format('%s (%d)',[Item.Name,Item.Size]);
  end,
  True );
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat