Page 1 of 1

CustomSortArray() + CustomSortList()

Posted: Fri Aug 25, 2023 11:05 am
by benubi
Hello

I thought it would be awesome if we could sort Lists and Arrays using a callback, a bit like there's the CustomFilterCallback for 2D Drawing. For Lists,Arrays, StructuredLists and StructuredArrays.

I didn't know about CompareStructure() but I suppose it will go from first element to last element, and not let's say only check element #3 and then #2. With a custom sort callback you could change the sort order how you like.

CustomSortArray( Array(), @Procedure(), *userdata)
CustomSortList( List(), @Procedure(), *userdata)

Prototype.i CompareItem(*Left, *Right, *userdata) ; returns -1 of *left comes first, 1 if *right comes first, 0 if both are equal

I'd suggest that the elements always get treated like structures, so that when you have a string List or Array, both parameters are of the type .STRING instead of .s, .QUAD instead of .q etc. so the prototype always fits.

This is not an urgent feature request, there are also multiple user libraries that would allow it, I wrote own things to do similar things - but I always have to use help arrays and a bit of detours to get to where I want, and it could be faster and prettier. Just an idea.

Re: CustomSortArray() + CustomSortList()

Posted: Fri Aug 25, 2023 11:57 am
by jacdelad
I am not sure if I'm right, but sorting is a bit more complicated than going from the first to the last element. My only proof for this claim is the the custom sorting function for listviews (ListIconGagdet in PureBasic terms) from the Windows API, which basically does what you want to achieve, but needs to compare some elements more than once. This of course heavily depends on the sorting algorithm, which always wants to achieve the fastest way to sort by doing as little comparisons/movements as possible (fastest known way is StalinSort).
So in conclusion I think it's better to write a sorting algorithm on your own, which works best for your specific sorting problem.

But as I said, I'm not entirely sure if this is really so complicated.

Re: CustomSortArray() + CustomSortList()

Posted: Fri Aug 25, 2023 12:08 pm
by Little John
+1 from me for the general feature request for custom sorting functions, that support a user defined callback for comparison of the elements.
I'm pretty sure this has been requested before. However, I currently don't have a link to a regarding older forum topic.

Re: CustomSortArray() + CustomSortList()

Posted: Fri Aug 25, 2023 12:30 pm
by benubi
@jacdelad

No it should be relatively "simple" to implement, the only problem being that the team would have to create those functions for the C and ASM backends (that would be around 5-7 versions I guess). But I don't know how PB handles it internally (multiple functions, one for each sorted data type, or one with Select #PB_Type loop). "Simple" but it may take some time.

In the CAV archive there are examples for quicksort etc. I modified some of those for my own experiments nearly a decade ago (perhaps even longer). I wanted to have a "general" sort algorithm for structures, but you'd have to declare/define the structure + it's elements first in some sort of structure descriptor. So it goes through an additional layer of abstraction which would be handled by PB normally. Once you have the structure described you could do (in my old mini lib) a Quicksort by using a sort/order string e.g. "+Username,-LastLogin,+UserLevel" or so, with the +/- indicating the ascending/descending sort order. This would become irrelevant with the custom sort algorithm because PB would only send pointers of the elements to the callback. So each callback should know how the structure is, but that should be trivial and be written in the procedure declaration (*Left.MyStructure, *Right.MyStructure, *udata). The *udata pointer or value could indicate a sort order or be used for debugging or other purposes.

One problem is that I don't know how to get pointers to internal Structure-Descriptors (those PB creates during compilation) or how to know how they work (it's proprietary/closed source). If I knew I could write those myself, but there would also still be the problem that Array() and List() parameters would be of a fixed type when done with pure PB code, so this would lead to write a sort function for every structure type which would miss the point. That's why I had to go through an "abstraction layer" where you'd have to define the structure on your own, which makes a bit everything redundant, and error prone because you may forget to update your definition string after you changed the structure...

And I became lazier over time :lol:

Re: CustomSortArray() + CustomSortList()

Posted: Fri Aug 25, 2023 3:55 pm
by STARGĂ…TE