Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include once "crt.bi"
- randomize timer
- type character_struct
- as string * 10 name
- as integer yPosition
- end type
- declare function char_sort_callback cdecl ( byval elm1 as any ptr, byval elm2 as any ptr ) as integer
- dim as character_struct characters(10)
- print "NOT SORTED..."
- for i as integer = 0 to ubound(characters)
- characters(i).yPosition = int(rnd*10000)
- print characters(i).yPosition
- next
- 'here's how to call the crt qsort on a custom type
- qsort( @characters(0), ubound(characters)+1, sizeof(character_struct), @char_sort_callback() )
- print
- print "SORTED..."
- for i as integer = 0 to ubound(characters)
- print characters(i).yPosition
- next
- sleep
- 'here's the meat of it...
- 'we're sorting based on the "yPosition" variable in the character type,
- 'which will sort every element of the whole array in turn.
- function char_sort_callback cdecl ( byval elm1 as any ptr, byval elm2 as any ptr ) as integer
- return sgn( (cptr(character_struct ptr, elm1)->yPosition) - (cptr(character_struct ptr, elm2)->yPosition) )
- end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement