Thema: Delphi FileListBox sortieren

Einzelnen Beitrag anzeigen

marabu

Registriert seit: 6. Apr 2005
10.109 Beiträge
 
#6

Re: FileListBox sortieren

  Alt 5. Jul 2005, 19:38
Vielleicht kommst du ja hiermit zurecht:

Delphi-Quellcode:
type
  THackedFileListBox = class(TFileListBox);

function PrefixLength(s: string): integer;
begin
  Result := Length(s);
  while (Result > 0) and (s[Result] in ['0'..'9']) do
    Dec(Result);
end;

function NaturalSortCompare(list: TStringList; index1, index2: integer): integer;
var
  prefix1, prefix2, suffix1, suffix2: string;
  n1, n2: integer;
begin
  suffix1 := AnsiUppercase(list[index1]);
  prefix1 := Copy(suffix1, 1, PrefixLength(suffix1));
  Delete(suffix1, 1, Length(prefix1));
  if not TryStrToInt(suffix1, n1) then n1 := -1;

  suffix2 := AnsiUppercase(list[index2]);
  prefix2 := Copy(suffix2, 1, PrefixLength(suffix2));
  Delete(suffix2, 1, Length(prefix2));
  if not TryStrToInt(suffix2, n2) then n2 := -1;

  if prefix1 < prefix2 then Result := -1 else
  if prefix1 > prefix2 then Result := 1 else
  if n1 < n2 then Result := -1 else
  if n1 > n2 then Result := 1 else
  Result := 0;
end;

procedure TDemoForm.ButtonClick(Sender: TObject);
var
  sl: TStringList;
begin
  sl := TStringList.Create;
  sl.Assign(FileListBox.Items);
  sl.CustomSort(NaturalSortCompare);
  THackedFileListBox(FileListBox).Sorted := false; // das hilft...
  FileListBox.Items.Assign(sl);
  sl.Free;
end;
Grüße vom marabu
  Mit Zitat antworten Zitat