Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C rtlib sorting question...
#1
I know nothing about this, but I read somewhere on this forum that it's nice & fast.

Is it possible to sort a udt array with it. I want to use it to sort the drawing order for a billboarding projectile engine.

I have a small udt...

Code:
Type Projectile_Sort
   Dist as Single
   Id as Integer
End Type

Dim Projectile(Num_Projectiles) as Projectile_Sort

Is there a way to make QSort swap both elements when Dist(i)<Dist(j)? Ummm... Like this...

Code:
Swap Dist(i), Dist(j)
Swap Id(i), Id(j)

Is there a way that I can make QSort do that?
Reply
#2
Sure, qsort() takes a callback as argument, so it can be used with pretty much any kind of data, ie (untested):

Code:
function ProjectileCompare cdecl (byref elm1 as Projectile, byref elm2 as Projectile ) as integer

  function = sgn( elm1.dist - elm2.dist )

end function

qsort( myarray, arrayitems, len( Projectile ), @ProjectileCompare )
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)