Forum > Beginners

Generic Array Sorting

<< < (2/4) > >>

TRon:

--- Quote from: Zvoni on February 12, 2024, 09:20:27 am ---EDIT: Just looked at the source: It actually IS a class Function....doh me  :-[

--- End quote ---
Yeah, these days you never can be sure what to expect. For old timers (like myself) it can (still) be rather confusing at times. There is that small voice in the back of the head that keeps telling something is wrong there.

Just saying that you are not the only one   :)

Nitorami:
The code was proposed by CodeConvert, including the call to the sort method in line 16 which does not work. It seems CodeConvert is ahead of its time, because the method is not yet implemented in Generics.Collections !?!

But anyway, I got the sorting to work with the TArrayHelper as TRon proposed. I'm an old timer myself, only just got used to type helpers, and now it confuses me that in the generic case I need to explicitly write TArrayHelper<sometype>.sort(MyArray) instead of MyArray.Sort. TRon says it has to do with the lack of anonymous functions; I don't understand this but guess I'll just accept it.

I still scratch my head about this sentence in https://www.freepascal.org/docs-html/3.2.0/rtl/sysutils/tarray.html:

"TArray [...] definition is not needed in Free Pascal, where 2 array types are equal if their element types are equal." What has the TArray definition to do with the question if two arrays are equal ???

Thaddy:

--- Quote from: Nitorami on February 12, 2024, 12:27:17 pm ---The code was proposed by CodeConvert, including the call to the sort method in line 16 which does not work. It seems CodeConvert is ahead of its time, because the method is not yet implemented in Generics.Collections !?!

--- End quote ---
No, you have to implement a comparison by yourself that is by design. There is no default. Recurring subject, answered many times...

Nitorami:
The wiki https://wiki.lazarus.freepascal.org/Data_Structures,_Containers,_Collections explicitly says The Generics Collections offers TArray with static methods for searching and sorting. That would be at least misleading; and in fact the sort method exists, and it works for integers, float and chars, probably more (not tested), but it must be called via a helper such as
--- 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";}};} ---TArrayHelper<mytype>.sort(MyArray)
I understand that I must define compare methods for my own specific types, but it would be embarrassing if everyone would have to reinvent this for simple types.

Thaddy:
yes for simple cases there are predefined options. That is correct.
But usually useless with more complex cases.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version