Forum > General
TFPGObjectList and custom sorting function
Чебурашка:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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<TItemData>; 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.
Чебурашка:
I found.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---result := integer(v > 0) - integer(v < 0); // -1, 0, or +1 is my recommended way. v = keys values. And it is bloody fast.
bytebites:
--- Quote from: Чебурашка on May 29, 2023, 04:23:06 pm ---I found.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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.
--- End quote ---
Line 3: Do you really want to compare Key2.F1 and Key1.F2?
Thaddy:
--- Quote from: bytebites on May 29, 2023, 10:35:41 pm ---Line 3: Do you really want to compare Key2.F1 and Key1.F2?
--- End quote ---
He probably does, but his code is way overcomplicated as I already showed.
Navigation
[0] Message Index
[#] Next page