This source code is a little on the flakey side.
1) It will only sort by databse fields and not calculated fields
2) The open query browse for each by field, doesnt take into account any where statements. No-one wants to retype the whole query, cant a preprocessor be used such as {&open-query{&browse-name}
If you manage to fix these problems it would be a big help
Regards