key = "D"; for (y = 1; y <= Bottom; y++) { count = 0; for (x = 1; x <= Right; x++) { if ([x,y] == key) { count++; } } for (x = Right; count > 0; x--) { if ([x,y] == key) { [x+count,y] = "Z"; count--; } [x+count,y] = [x,y]; } }