Page 1 of 1

Callback for Sorting-commands to allow custom sorting orders

Posted: Mon Jul 27, 2009 12:11 am
by AND51
Hello!

What do you think of the idea to implement the possibility for a callback for all the sorting commands in order to allow the coder to use its own order?

Example:

Code: Select all

Procedure callback(a, b)
	; your own algorithm goes here
	If a < b
		ProcedureReturn #PB_String_Lower ; -1
	ElseIf a = b
		ProcedureReturn #PB_String_Equal ; 0
	Else
		ProcedureReturn #PB_String_Greater ; 1
	EndIf
EndProcedure

SortList(myList(), #PB_Sort_Ascending, @callback())
The callback should return -1, 0 or 1 for lower, equal or greater results.
The new syntax for SortList(), for example could be

Code: Select all

SortList(ListName(), Options [, @Callback() [, Start, End]])
Whereas it could be 0 to leave it out while using Start/End scope.

Posted: Mon Jul 27, 2009 12:21 pm
by luis
Yes, it would be nice. I have implemented it this way in the quicksort routine I use. Obviously there is a little penalty in calling the compare procedure.
But personally I don't consider it a problem.

But I am a BIG fan of callback procedures in general. :)

Maybe there are sort routines like that in the forum too, I think I saw something of that kind here before.

Posted: Tue Jul 28, 2009 3:57 am
by AND51
> Obviously there is a little penalty in calling the compare procedure
There will be a little penalty, because you need a few steps to call the procedure; but most of the time is being spent on the algorithm of the coder. Not matter how efficient he/she will code, it will take a certain amount of time.

It would be interesting to hear, which algorithm PB uses to sort the content. Can you tell us, PB team? I can imagine, that there are algorithms that are efficient, but need much less steps than other algorithms.
In other words: the less steps the internal sorting algorithm needs, the more time can be spent on the callback; the time needed will be the same as using no callback and an algorithm that takes more steps.
Hope, you could follow me.

> But personally I don't consider it a problem
Me too. PackMemory(), for example, also accepts callbacks. I bet, a callback there also creates a little penalty, however, you would never get the idea that the (de)compressions takes much more time, just because of the callback.

> Maybe there are sort routines like that in the forum too
Well, it would be neat to have this natively implemented. :wink:
That's the reason for posting it into the feature request subforum. If we always take any codes from the forum, we could close the feature request subforum. :lol:

Posted: Tue Jul 28, 2009 12:00 pm
by gnozal
AND51 wrote:It would be interesting to hear, which algorithm PB uses to sort the content. Can you tell us, PB team? I can imagine, that there are algorithms that are efficient, but need much less steps than other algorithms.
Iirc, PB 4.3x uses Mergesort for SortStructuredList() / SortList() and Quicksort for SortArray().

Posted: Tue Jul 28, 2009 3:00 pm
by AND51
Cool, thanks. I'll google them to read their description.
By the way: to be honest, this feature request was empowered by your PureLVSORT library, where you can also set a custom callback for sorting a ListIconGadget(). :wink: