Fix infinite recursion in SqlPriorityQueue

Fixes Issue #3099.
By moving the compare function up the chain and only setting flags bottom up, the code stays maintainable and prevents dirty circular references. Those nasty refs should be a last resort.