Is there a quick way to sort a stringlist on it's .ValueFromIndex field instead of it's .Names field?
James

Mailing lists / fpc-pascal / 2022-02
4 messages · 2 people ·
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Is there a quick way to sort a stringlist on it's .ValueFromIndex field instead of it's .Names field?
James
James Richters wroteIs there a quick way to sort a stringlist on it's .ValueFromIndex field instead of it's .Names field?
// Custom compare function
function CompareValues(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result:=CompareText(List.ValueFromIndex[Index1],List.ValueFromIndex[Index2])
end;
// Sort using custom compare function
Mylist.CustomSort(@CompareValues);Michael.
Thanks for the elegant solution Michael, but I'm having some issues implementing it.
I'm getting:
pastep.pas(28651,10) Warning: Function result does not seem to be set
28650 // Custom compare function
28651 function CompareValues(List: TStringList; Index1, Index2: Integer): Integer;
28652
28653 begin
28654 Result:=CompareText(List.ValueFromIndex[Index1],List.ValueFromIndex[Index2])
28655 end;
pastep.pas(28797,65) Error: Incompatible type for arg no. 1: Got "Pointer", expected "<procedure variable type of function(TStringList;LongInt;LongInt):LongInt;Register>"
28797 AllLogLastRunStringList.CustomSort(@CompareValues);
The unit I'm putting this in has:
Unit PAStep;
{$Mode TP}{$I-}
{$modeswitch exceptions}
{$R+}
I can fix the first error with:
28654 CompareValues:=CompareText(List.ValueFromIndex[Index1],List.ValueFromIndex[Index2])
{$Mode TP} doesn't like using 'result'But I have no idea how I could fix the second error. Any ideas?
James
Ehmmm..
TStringList is not supposed to be used in mode TP, and Result likewise should not be available IIRC...
Try to remove the @
Michael.