def sortSparseMatrix(m, rev=True, only_indices=True):
    """ Sort a sparse matrix and return column index dictionary
    """
col_dict = dict()
for i in range(m.shape[0]):
    d = m.getrow(i)
s = zip(d.indices, d.data)
sorted_s = sorted(s, key=lambda v: v[1], reverse=True)
if only_indices:
    col_dict[i] = [element[0] for element in sorted_s]
else:
    col_dict[i] = sorted_s
return col_dict
Sorting in Sparse Matrix