Forum > General

Sort Comparer function

(1/4) > >>

jcmontherock:
Hello,
I have a TList containing a class of data fields:

--- 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";}};} ---  TDataClass = Class    drInteger:    Integer;    drFloat:      Double;    drCharArray:  Array[0..400] of Char;    drDateTime:   TDateTime;    drString:     String[100];  end;For sorting this list, I'am using TList.Sort with a comparer function.
 
Is somebody knows how to pass some parameters directly to the comparer function.
For ex. in that function I want know on which field of class, list will be sorted, and in which direction (ascending or descending).

Windows 11/64 Lazarus 222/64

Zvoni:
Use an Enum indicating the Field and a Boolean for Direction?

MarkMLl:

--- Quote from: Zvoni on August 03, 2022, 08:40:28 am ---Use an Enum indicating the Field and a Boolean for Direction?

--- End quote ---

I suspect that he's asking about "best practice" for passing additional parameters into the comparison function, i.e. not provided for by the type that defines it.

Without a compilable example it's difficult to be sure.

MarkMLl

jcmontherock:
Find here an example. I want to have only one sort comparer function instead one for each type of field.

MarkMLl:

--- Quote from: jcmontherock on August 03, 2022, 05:49:07 pm ---Find here an example. I want to have only one sort comparer function instead one for each type of field.

--- End quote ---

I think the interesting approach would be to move the comparer inside an instance of (a subclass of) TFPList, and have it refer to a field of the instance to find out how it should be sorting. I just don't know yet whether that could be done...

The /proper/ way is probably to subclass TFPList, and redefine Sort such that the comparer took an extra parameter. I don't know whether that could be done concisely.

If nobody else comes up with anything I'll try to have a play later.

MarkMLl

Navigation

[0] Message Index

[#] Next page

Go to full version