Custom form, custom template or preprocess function

Having custom template or function to preprocess your custom form in module has few tricks.

First you need to define it in form

$form = array();
$form['#theme'] = 'my_form_theming_function';
....

then define hook_theme

Altering weight of elements in arrays used for rendering

When changing weight of element in array, it is not enough to just change weight, #sorted must be set to FALSE so elements are resorted.

$variables['form']['element']['#weight'] = -10;
$variables['form']['element']['#sorted'] = FALSE;