fix count in iter_sort_index
refactoring code generator