0

I created a CustomSort for a list with TStringLinst. On Windows works fine, but on Android List object is sent as nil to the CompareDatesReverse function. In the documentation there is no any difference between the operating systems to this function. Is there any different handling for the CustomSort function on Android?

Parameter List = nil in the CompareDatesReverse function on Android => Error: Exception class Segmentation fault (11)

   TObjAux = class(TObject)
      FileAge: TDateTime;
      Path: String;
   end;

procedure TForm1.Button1Click(Sender: TObject);
   function CompareDatesReverse(List: TStringList; Index1, Index2: Integer): Integer;
   var d1, d2: TDateTime;
   begin
      d1 := TObjAux(List.Objects[Index1]).FileAge;  // List = Nil on Android
      d2 := TObjAux(List.Objects[Index2]).FileAge;
      if d1 > d2 then
         Result := -1
      else if d1 < d2 then
         Result := 1
      else
         Result := 0;
   end;
var
   LList: TStringDynArray;
   i: Integer;
   fls: TStringList;
   vTmp: String;
   ObjAux: TObjAux;
begin
   fls := TStringList.Create;
   fls.Clear;

   vTmp  := TPath.GetDocumentsPath;
   LList := TDirectory.GetFiles(vTmp);

   for i := 0 to Length(LList) - 1 do begin
      ObjAux      := TObjAux.Create;
      ObjAux.Path := LList[i];
      FileAge(LList[i], ObjAux.FileAge);
      fls.AddObject(FloatToStr(ObjAux.FileAge), ObjAux);
   end;

   if fls.Count > 0 then fls.CustomSort(@CompareDatesReverse);

   fls.DisposeOf;
end;
wBB
  • 821
  • 1
  • 18
  • 39
  • That's pretty darn yucky. Why don't you use a `TList` with each item being a record containing the data. Then you don't need to pointlessly convert file age to a string. – David Heffernan Jan 10 '18 at 19:45
  • 1
    But as for the error, the use of the @ operator is where to start. Remove that, and then fix the compiler error. Which will involve not using a nested function. Don't use the @ operator with procedures and functions. It just stops the compiler from saving you. I'll close this against the standard duplicate next. – David Heffernan Jan 10 '18 at 19:48
  • 1) You're right about duplicate question; 2) Retraining the nesting of the `CompareDatesReverse` function works on Android and Windows, thanks! 3) However, I didn't understand how to remove `@` in this case. I read your documentation of the other issue, but even after a few hours, I still couldn't modify my code to work as you suggested (without `@`) 4) Good `TList` idea and I'll use it as soon as I can solve the main problem. Can you help me solve it? – wBB Jan 11 '18 at 02:12
  • Forget it! It works without `@` now. I will implement the `TList` issue which is cool! Thank you. – wBB Jan 11 '18 at 02:38

0 Answers0