Registriert seit: 6. Apr 2005
10.109 Beiträge
|
Re: Natürliche Sortierung
19. Jun 2005, 13:24
Delphi-Quellcode:
function After(S, Pattern: String): String;
var
I: Integer;
begin
I := Pos(Pattern,S);
if I = 0
then Result := ''
else Result := Copy(S, I + Length(Pattern), Length(S));
end;
function Before(S, Pattern: String): String;
var
I: Integer;
begin
I := Pos(Pattern,S);
Delete(S, I, Length(S));
Result := S;
end;
function ZPad(S: String; Size: Integer): String;
begin
while (Length(S) < Size) and (Size < 256) do
S := '0' + S;
Result := S;
end;
function SortCompare(List: TStringList; Index1, Index2: Integer): Integer;
const
DIGITS = 4;
var
item1, item2: string;
begin
item1 := List[Index1];
item2 := List[Index2];
if ZPad(Before(item1, 'x'), DIGITS) < ZPad(Before(item2, 'x'), DIGITS) then Result := -1 else
if ZPad(Before(item1, 'x'), DIGITS) > ZPad(Before(item2, 'x'), DIGITS) then Result := 1 else
if ZPad(After(item1, 'x'), DIGITS) < ZPad(After(item2, 'x'), DIGITS) then Result := -1 else
if ZPad(After(item1, 'x'), DIGITS) > ZPad(After(item2, 'x'), DIGITS) then Result := 1 else
Result := 0;
end;
Aufruf mit "StringList.CustomSort(SortCompare)"
Grüße vom marabu
|
|
Zitat
|