We should also sort the words and numbers, noting the position of words and numbers in the input sequence, and then write them to the output in accordance with this order.
Sort is a built-in language feature, instead of writing their own, if this is not required by the problem statement.