Fixed --order-by broken by namespaces refactoring

Also added a regression test for it.

Bug and patch from Beena Emerson, thany you.