Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2022-02

Sorting a Stringlist on ValueFromIndex

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Richters

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

James

Michael Van Canneyt
to James Richters
James Richters wrote
Is 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.

James Richters
to Michael Van Canneyt

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

Michael Van Canneyt
to James Richters
James Richters wrote 20 lines
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-}

Ehmmm..
TStringList is not supposed to be used in mode TP, and Result likewise should not be available IIRC...

Show quoted text 8 lines
{$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?

Try to remove the @

Michael.