One method is to rotate the image 90 degrees, sort its rows horizontally, then rotate the image back again.
One method is to loop over columns and march down, increasing the pixels[] index by width
each time (that moves to the pixel below it).
You may not want to do your sort operation every single frame of draw. Perhaps once in setup, and / or on a keypress?