Własne sortowanie TStringList

0

Nie mogę sobie poradzić z CustomSort.

Oto relewantny fragment kodu (idea: posortowanie alfabetyczne listy plików wg nazw niezależnie od ścieżki):

procedure TForm1.ButtonGoClick(Sender: TObject);
{...}
if OpenDialog1.Execute then
begin
Process(OpenDialog1.Files as TStringList, RadioGroupFileType.ItemIndex);
{...}
end;

procedure TForm1.Process(Source: TStringList; FileType:Integer);
var
i, diff: integer;
prefix, newname: string;
begin
Source.Sorted:=False;
{...}
Source.CustomSort(@TForm1.SortNames);
{...}
end;

function TForm1.SortNames(List: TStringList; Index1, Index2: Integer): Integer;
stdcall;
begin
Result:=CompareStr(UpperCase(ExtractFileName(List[Index1])),
UpperCase(ExtractFileName(List[index2])));
end;

powyższy kod wywala Index out of bounds. Próbowałem bez stdcall, wtedy Access violation. Bardzo proszę o pomoc, możliwie szybko.

0

procedure TForm1.Process(Source: TStringList; FileType:Integer);
var
i, diff: integer;
prefix, newname: string;
begin
Source.Sorted:=False;
{...}
Source.CustomSort(@SortNames);
{...}
end;
function SortNames(List: TStringList; Index1, Index2: Integer): Integer;;
begin
Result:=CompareStr(UpperCase(ExtractFileName(List.items[Index1])),
UpperCase(ExtractFileName(List.items[index2])));
end;

chyba jakoś tak

0

var
S: TCustom.... //nie pamiętam jaki to typ;
begin
S := SortNames;
Source.CustomSort(S);

function SortNames(List: TStringList; Index1, Index2: Integer): Integer;

Nie możesz deklarować sekcji public ani private. To musi być poza obiektem. To jest zwykła funkcja a nie method of object. Nie sprawdzałem tego bo późno, ale o ile pamiętam to takie numery przy tym są.

[Dopisane]
A tu dla potomnych:
http://vcl.4programmers.net/viewmethods.php?id=246

0

Dryobates jak zwykle niezrównany [browar] [browar] [browar]

Typ nazywa się TStringListSortCompare

używać bez stdcall

1 użytkowników online, w tym zalogowanych: 0, gości: 1