As the centroids are not exactly (to the atomic level) aligned in a perfect North-South / East-West grid, you need to allow some displacement: if the points longitude/latitude are within X meters (cm, km, whatever) from each other, consider they are aligned.
For rectangular cell, don't bother with the centroid but you can use directly the min X or Y coordinate. Else you can replace
ORDER BY round(ST_MinY(geom)/50) desc,
) as newID
Note that I haven't used
ceil but I have rounded the coordinates divided by my distance of tolerance. This is done so
200.00, as well as
150.00 are considered the same, but
199.99 are considered different.
You may need to play a bit with this tolerance to find the perfect one if the grid cells have different sizes.