I'm confused by your sorting. I think the priorityqueue is the only thing doing the sorting by using the compare function you passed in. it should return its results sorted from lowest to highest. I'm not following why you're ordering things and putting negative numbers in places.