Well first you should pick one of the men to use as a starting point..
I will give it a boolean variable Startingpoint for this example
Also I'll add a boolean variable IsSorted
also I wil use two global variables StartingpointX and StartingpointY
on button pressed
----men is startingpoint
system set startingpointX = men.x
system set startingpointY = men.y
men set IsSorted to true
-- system repeat 5 times
---- pick all men
---- men Issorted is not true
------- men pick nearest to startingpointX, startingpointY
men set position to startingpointX, startingpointY+3
men set IsSorted to yes
system add 3 to startingpointY
easiest way would be to create a function for this and call it with an action.
comparing if men.count is not the same as the number of men with IsSorted is true
After the repeat 5 times you could subtract from the startingpointX to get to the new row of men.