Sorting: BUGFIX: sorted_index_list(): assign index_n only when present

* Sorting: BUGFIX: sorted_index_list(): assign index_n only when present:
  * use loc_index_n locally
  * only assign to index_n if optional dummy variable is present