changing sorting function to use functional programming via lambda functions