Forum > Beginners
Generic Array Sorting
TRon:
--- Quote from: Nitorami on February 12, 2024, 12:27:17 pm ---TRon says it has to do with the lack of anonymous functions; I don't understand this but guess I'll just accept it.
--- End quote ---
Delphi allows for generic TArray to do things like:
--- 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";}};} ---TArray.Sort<TNode>( Node, TComparer<TNode>.Construct( function(const Left, Right: TNode): Integer begin Result := TComparer<Double>.Default.Compare(Left.HCost, Right.HCost); end )); Which currently is not possible to realize with FPC due to lack of anonymous functions. It is thus not possible (yet) to make a generic such as TArray 100% compatible with Delphi. Unfortunately even with FPC generics implementation there are (still) some issues (see bugtracker reports) that need to be resolved or implemented. For example I am personally looking forward for typehelpers being able to be specialized/generalized.
Thaddy:
It works in trunk/main, though,
TRon:
True Thaddy but PascalDragon did the feature announcement ... what ... a couple of months ago or so ? (I might be mistaken on the time-line there)
Thaddy:
PascalDragon also explicitly stated the availability of the feature in trunk, not in release. Before it is in a release we have to wait and as PascalDragon wrote, that can take some time.
kjteng:
I have done some research recently to develop a generic array with some methods (quicksort, indexof, commaText etc) for my own purpose. Incidentally, I have also tried certain of the methods provided in generics.collections unit. I noticed that we can actually assign a custom compare method for the TArrayHelper/sort as follows:
--- 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";}};} ---// custom compare methodfunction CompareMyRec(constref X, Y: TMyRecType): integer;begin ...end; // to call sort method with custom compare method:-Specialize TArrayHelper<TMyRecType>.Sort(ArrayOfMyRec, Specialize Comparer<TMyRecType>.Construct(@CompareMyRec)); The above will override the default compare method for simple data type e.g. integer, ansistring, etc).
Navigation
[0] Message Index
[#] Next page
[*] Previous page