added sorted key helper function for part C