Using a flat index for sorting may overflow int32, so care should here be taken in choosing the integer type.