refactor of sort_indices_* functions based on Bob Carpenter's https://github.com/stan-dev/stan/pull/564#issuecomment-35143532
Adjustments in rev and fwd diff for the refactoring of sort_indices_* functions