'Sort features by name
Returns a reordered matrix and modifies the vocabulary in place'

def _sort_features(self, X, vocabulary):

    sorted_features = sorted(six.iteritems(vocabulary))
    map_index = np.empty(len(sorted_features), dtype=np.int32)
    for (new_val, (term, old_val)) in enumerate(sorted_features):
        vocabulary[term] = new_val
        map_index[old_val] = new_val
    X.indices = map_index.take(X.indices, mode=u'clip')
    return X
