You can't do sort foo > foo because you'll be left with a blank file.
Although sort(1) handles this for you as you mention, the general problem in the shell of wanting to overwrite your input file with the output file, but not being able to redirect to it for the reason you mention, is solved with sponge. Rather than:
$ grep "foo" bar.txt >baz.txt; mv baz.txt bar.txt
instead:
$ grep "foo" bar.txt | sponge bar.txt
You can't do sort foo > foo because you'll be left with a blank file.