Sort via a pointer to a pkg comparison function

Applied all the changes suggested by the Author.