Some changes that hopefully solve a sort exception with java 7 (and endl...

...ess loop in delivery manager)

* only sort queue if their are some entries
* sort queue after we got the lock
* use Long.compareTo instead of subtraction and casting which can produce a overflow