Fully commented new node sorting algorithm