Skip to content

Instantly share code, notes, and snippets.

@hernad
Created December 7, 2008 09:40
Show Gist options
  • Save hernad/33071 to your computer and use it in GitHub Desktop.
Save hernad/33071 to your computer and use it in GitHub Desktop.
require 'ffi'
module LibC
extend FFI::Library
callback :qsort_cmp, [ :pointer, :pointer ], :int
attach_function :qsort, [ :pointer, :int, :int, :qsort_cmp ], :int
end
p = MemoryPointer.new(:int, 2)
p.put_array_of_int32(0, [ 2, 1 ])
puts "Before qsort #{p.get_array_of_int32(0, 2).join(', ')}"
LibC.qsort(p, 2, 4) do |p1, p2|
i1 = p1.get_int32(0)
i2 = p2.get_int32(0)
i1 < i2 ? -1 : i1 > i2 ? 1 : 0
end
puts "After qsort #{p.get_array_of_int32(0, 2).join(', ')}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment