Handle unorderable nodes in functions using heapq

#1111