TFPGObjectList and custom sorting function lazarus / 62-General ---------------------------------------------------------------------- Чебурашка · 2023-05-29 14:11 Given this simple class and the related TFPGObjectList, what is the "recommended" way of implementing a custom sorting function (for example, the one shown in the comparer function below)? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit simple; interface uses  classes, sysutils, fgl; type  TItemData = class(TObject)  public    F1, F2, F3: string;       constructor Create(A1, A2, A3: string); reintroduce;  end;  type  TItemDataList = specialize TFPGObjectList; implementation constructor TItemData.Create(A1, A2, A3: string);begin  inherited Create();   F1 := A1; F2 := A2; F3 := A3;end; (* function CompareItemData(Key1, Key2: Pointer): Integer;var  x1, x2: TItemData;begin  x1 := TItemData(Key1);  x2 := TItemData(Key2);   if x1.F1 < x2.F1 then Result := -1  else if x2.F1 < x1.F2 then Result := 1  else    if x1.F2 < x2.F2 then Result := -1    else if x2.F2 < x1.F2 then Result := 1  else      if x1.F3 < x2.F3 then Result := -1      else if x2.F3 < x1.F3 then Result := 1      else        Result := 0;end;*) end. ---------------------------------------------------------------------- Чебурашка · 2023-05-29 14:23 I found. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function CompareItemData(const Key1, Key2: TItemData): Integer;begin if Key1.F1 < Key2.F1 then Result := -1  else if Key2.F1 < Key1.F2 then Result := 1  else    if Key1.F2 < Key2.F2 then Result := -1    else if Key2.F2 < Key1.F2 then Result := 1    else      if Key1.F3 < Key2.F3 then Result := -1      else if Key2.F3 < Key1.F3 then Result := 1      else      Result := 0;end; // AFPGObjectList.Sort(@CompareItemData);   Sorry for the noise. ---------------------------------------------------------------------- Thaddy · 2023-05-29 15:50 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i result := integer(v > 0) - integer(v < 0); // -1, 0, or +1 is my recommended way. v = keys values. And it is bloody fast. ---------------------------------------------------------------------- bytebites · 2023-05-29 20:35 Quote from: Чебурашка on May 29, 2023, 04:23:06 pmI found. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function CompareItemData(const Key1, Key2: TItemData): Integer;begin if Key1.F1 < Key2.F1 then Result := -1  else if Key2.F1 < Key1.F2 then Result := 1  else    if Key1.F2 < Key2.F2 then Result := -1    else if Key2.F2 < Key1.F2 then Result := 1    else      if Key1.F3 < Key2.F3 then Result := -1      else if Key2.F3 < Key1.F3 then Result := 1      else      Result := 0;end; // AFPGObjectList.Sort(@CompareItemData);   Sorry for the noise. Line 3:  Do you really want to compare Key2.F1 and Key1.F2? ---------------------------------------------------------------------- Thaddy · 2023-05-30 02:28 Quote from: bytebites on May 29, 2023, 10:35:41 pmLine 3:  Do you really want to compare Key2.F1 and Key1.F2? He probably does, but his code is way overcomplicated as I already showed. ---------------------------------------------------------------------- Чебурашка · 2023-05-30 06:32 Quote from: bytebites on May 29, 2023, 10:35:41 pmLine 3:  Do you really want to compare Key2.F1 and Key1.F2? It's obviously a typo. But problem was not the logic of the compare method, but the compare method firm, because initially I used Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function CompareItemData(Key1, Key2: Pointer): Integer;  where I should use Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function CompareItemData(const Key1, Key2: TItemData): Integer; ---------------------------------------------------------------------- Чебурашка · 2023-05-30 06:36 Quote from: Thaddy on May 29, 2023, 05:50:31 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i result := integer(v > 0) - integer(v < 0); // -1, 0, or +1 is my recommended way. v = keys values. And it is bloody fast. Thaddy could you please you explain how to use this in the context of the specific class, where fields to be compared are the various strings in the ordered way shown? I am _very_ interested in speed because in certain cases I will have to sort huge amounts of data, so any improvement will help me. Thank you ---------------------------------------------------------------------- jamie · 2023-05-30 11:16 i Belieave all you need to do is subtract one from the other, there is no need to set the actual output to -1,0 or 1,  the body code from outside the sort should be looking for  <0,0 and of course anything above 1.  go in steps,  Result := key1.F1-Key2.F1;  If Result = 0 then  result := key1.F2-Key2.F2;  If result = 0 then......  etc .. I think you get what I am talking about. ---------------------------------------------------------------------- Чебурашка · 2023-05-30 11:21 Quote from: jamie on May 30, 2023, 01:16:20 pmi Belieave all you need to do is subtract one from the other In my example the fields are strings, is subtracting strings possible? Anyway thanks ta everybody, I probably missed to explain that my problem was not the compare function logic, but its firm. I was getting compile error before turning it in the correct form. ---------------------------------------------------------------------- jamie · 2023-05-30 11:38 yes you can, but I don't know extent as to how much the fpc compiler actually does the test. B := Integer(S1 < S2); B being a Integer, S1 and S2 being a string. I just assumed you were doing integers. For Strings, you can use a already built functions in libs to generate the -1,0,1 values comparing strings. I would need to look that up.