Recent

Author Topic: [SOLVED] TStringlist Custom Sort  (Read 5231 times)

Boboxx

  • New member
  • *
  • Posts: 9
[SOLVED] TStringlist Custom Sort
« on: September 01, 2016, 03:42:57 pm »
I'm trying to do a custom sort on a TStringList and i'm geting a error:

function TFrmTimer.CompareFloat(List: TStringList; Index1, Index2: Integer): Integer;
begin
  Result := StrToInt(List[Index1]) - StrToInt(List[Index2]);
end;


procedure TFrmTimer.Button5Click(Sender: TObject);
var
  Str: TStringList;
  i, int: integer;
begin
   Randomize;
   Str := TStringList.Create;

   for i := 1 to 10 do
    begin
     Str.Add(IntToStr(Random(100)));
    end;
   Memo1.lines.Clear;
   Memo1.Lines.add(intToStr(Str.Count));
   Memo1.lines := Str;

   Str.CustomSort(@CompareFloat);
   Memo1.Lines.add('sorted-------------------');
   Memo1.lines := Str;
   Str.Free;
end; 

But I'm getting a error that I don't understand:
main.pas(374,32) Error: Incompatible type for arg no. 1: Got "<procedure variable type of function(TStringList;LongInt;LongInt):LongInt of object;Register>", expected "<procedure variable type of function(TStringList;LongInt;LongInt):LongInt;Register>"
« Last Edit: September 02, 2016, 04:05:43 pm by Boboxx »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12562
  • FPC developer.
Re: TStringlist Custom Sort
« Reply #1 on: September 01, 2016, 03:55:11 pm »
You are passing a method that belongs to a class to something that expects a standalone procedure (not part of a method)

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: TStringlist Custom Sort
« Reply #2 on: September 01, 2016, 03:55:28 pm »
Since i am responsible:
1) a sortroutine can not be a procedure that is part of an object (you're is by means of your form)
2) i find the TFrmTimer part a bit scary. do you really have set a timer to run this routine ? If you do, then please don't

In case you really would like to use a timer then put inside your timer event:
Code: [Select]
Str.CustomSort(@CompareFloat);

and place your routine:
Quote
function CompareFloat(List: TStringList; Index1, Index2: Integer): Integer;
begin
  Result := StrToInt(List[Index1]) - StrToInt(List[Index2]);
end;
somewhere else

 

TinyPortal © 2005-2018