Callback for Sorting-commands to allow custom sorting orders

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Callback for Sorting-commands to allow custom sorting orders

Post 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.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Post 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.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post 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:
PB 4.30

Code: Select all

onErrorGoto(?Fred)
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post 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().
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post 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:
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Post Reply