Change text gravity like the 2048 puzzle

Jelly, 23 bytes

UZ
Ç¡=⁶$Þ€Ç$⁴¡ZU$⁵+⁴¤¡Y

Try it online!

I'm a bit unsatisfied, but MATL needed some competition. :P

Uses the order URDL. Inputs:

  • the input array as an array of padded lines
  • the number of repetitions
  • the move to start from (1 = U, 2 = R, 3 = D, 4 = L)

Explanation

UZ       Helper link. Argument: A (the 2D array)
U        Reverse each line and...
 Z       ...transpose. Rotates 90° CCW.

Ç¡=⁶$Þ€Ç$⁴¡ZU$⁵+⁴¤¡Y    Main link. Arguments: A, n (2D array, repetitions)
Ç                       Rotate 90° CCW...
 ¡                      ...m times. (m = which move to start on)

     Þ                  Sort...
      €                 ...each line of the array...
  =⁶                    ...based on the characters' equality to " ".
       Ç                Rotate 90° CCW.
        $               Combine the sort and rotate to one action.
         ⁴¡             Do that n times. (n = repetition count)

           Z            Transpose and...
            U           ...reverse each line. Rotates 90° CW.
             $          Combine the transpose and reverse to one action.
                  ¡     Do that...
              ⁵+⁴¤      ...m + n times.

                   Y    Join the array by newlines.

JavaScript (ES6), 168 bytes

(n,d,s,t=s.replace([RegExp(`( )([^]{${l=s.search`
`}})(\\w)`),/(.)(\b)( )/,RegExp(`(\\w)([^]{${l}})( )`),/( )(\b)(.)/][d%4],`$3$2$1`))=>n?t!=s?f(n,d,t):f(n-1,d+1,s):s

Ungolfed:

function gravity(count, direction, string) {
    let width = string.indexOf('\n');
    let up = new RegExp('( )([^]{' + width + '})(\\w)');
    let down = new RegExp('(\\w)([^]{' + width + '})( )');
    while (count--) {
        let regexp = [up, /(.)(\b)( )/, down, /( )(\b)(.)/][direction++ % 4];
        while (regexp.test(string)) string = string.replace(regexp, '$3$2$1');
    }
    return string;
}

d is the initial index into the directions which are URDL.