Recent

Author Topic: TStringList.CustomSort implementation  (Read 4458 times)

Ronan

  • Full Member
  • ***
  • Posts: 132
TStringList.CustomSort implementation
« on: February 21, 2014, 12:21:32 pm »
Dear All,

I'm trying to implement the CustomSort in TstringList class, with another more efficient sorting algorithm but I'm unable to do so .

// The code declaration is as follow
Code: [Select]
function CompareFloat(List: TStringList; Index1, Index2: Integer): Integer;

Code implementation and embedding it into CustomSort as callback function raises the following erros described below
Code: [Select]
var
   SL :TstringList;
SL.CustomSort(CompareFloat);
//SL.CustomSort(@CompareFloat); // Tried this one also
The first function call 'SL.CustomSort(CompareFloat)' retrieves that error from compiler "Error: Wrong number of parameters specified for call to "CompareFloat"

Second function call 'SL.CustomSort(@CompareFloat)' retrieves that error from compiler Error: Only class methods can be referred with class references

Regards,

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TStringList.CustomSort implementation
« Reply #1 on: February 21, 2014, 03:11:32 pm »
The first syntax without "@" indicates that you are in {$mode objfpc} so the address-of operator is required when referencing procedural types.

The second error may mean your sl.Customsort(@...) call is not within a normal
Code: [Select]
begin
...
end;
block as part of 'ordinary' code within a method somewhere (the snippet you give shows the call coming immediately after a var declaration - not sure if this is an exact copy of your code or not).

Ronan

  • Full Member
  • ***
  • Posts: 132
Re: TStringList.CustomSort implementation [SOLVED]
« Reply #2 on: February 22, 2014, 08:16:30 am »
Thank you in advance,
original code is as shown below, in any case that issue is solved by setting the mode to delphi,
Code: [Select]
SL :TStringList;
 str :string;
 i:Integer;
begin
  SL := TStringList.Create;
  SL.Add('8.3875');
  SL.Add('10.456');
  SL.Add('100');
  SL.Add('15');
  SL.Add('0.005');

  SL.CustomSort(CompareFloat);

Another question arises in mind related with the operation of CustomSort function, what the index1 and index2 indicates in function
Code: [Select]
CompareFloat(List: TStringList; Index1, Index2: Integer): Integer; are those two indices subsequent pointer to the list content?

Regards,

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: TStringList.CustomSort implementation
« Reply #3 on: February 22, 2014, 09:22:00 am »
Another question arises in mind related with the operation of CustomSort function, what the index1 and index2 indicates in function
Code: [Select]
CompareFloat(List: TStringList; Index1, Index2: Integer): Integer; are those two indices subsequent pointer to the list content?
http://www.freepascal.org/docs-html/rtl/classes/tstringlist.customsort.html[

 

TinyPortal © 2005-2018