Your current code is not sorting pixels horizontally. It loops over every pixel horizontally and, if it passes a brightness check, copies the previous value into it. This creates streaks.
I haven’t testsd it but there are some strange things about this code. First, it starts at height
pixels, so it skips a small part of the picture (it should start at 1).
Second, it isn’t a 2D loop. Because it is 1D it blends the left and right edges where it wraps around.
Third, it performs a test that can only fail if the brightness (0-255) is less than the threshold (which increases from 0-150000). So it only checks the first 26 pixels, then fails for the next 14974 pixels. If it succeeds, it copies an arbitrary pixel [height] onto the value. I’m not sure that this ever happens…