How to create sortable checkboxes using the form API? I'm using Drupal 7 and I need the ability for the user to choose from a list of options and put those options in order. I am using type checkbox and passing in an array of options to create the list. 

I need to be able to check the options needed and sort them. 

The form checkbox creation is fairly straightforward, but doesn't appear to have a weight option for the individual checkboxes or really any attributes except value. 

Any help would be appreciated. Thank you. 

$form[$key]['articleFields'] = array(
    '#title' =&gt; t('article Fields'),
    '#type' =&gt; 'checkboxes',
    '#description' =&gt; t('Choose the fields you want selected'),
    '#options' =&gt; $articleFields,
    '#default_value' =&gt;$articleFields_checked,

);
I was able to get this working using a modified version of the code here http://dropbucket.org/node/204

The adjustments I made were to allow my table to be an element on a parent form and to repopulate the from my variables array...
//TODO bubble checked boxes to top

    $rows = array();
    $row_elements = array();
    $data=array();
    //$shared['articleFields'] values stored in variables table -- loaded in parent form 
    //$key is name of my parent form this gets attached to

    $cc=-10; //default weights starting at -10
    //field machine names with titles 
    $articleFields = array(
      'title'=&gt;t('Headline'),
      'field_news_categories'=&gt;t('News Categories'),
      'field_text_2'=&gt;t('Subheading'),
      'field_photo_caption_1'=&gt;t('Featured Photo'),
      'body'=&gt;t('Copy'),
      'field_text_4'=&gt;t('Featured Quote'),
      'field_keywords'=&gt;t('Keywords'),
      'field_websites'=&gt;t('Related Links'),
      'field_images'=&gt;t('Additional Images'),
      'field_slideshow'=&gt;t('Slideshow'),
      'field_video'=&gt;t('Video'),
      'scheduler_settings'=&gt;t('Scheduler'),
      'group_alternates'=&gt;t('Alternates'),
      'field_text_1'=&gt;t('Alternate Headline'),
      'field_text_3'=&gt;t('Alternate Subheading'),
      'field_photo_caption_2'=&gt;t('Alternate Photo'),
      'field_longtext_1'=&gt;t('Alternate Copy'),
    );

    foreach ($articleFields as $k =&gt;$v) {
        // Build the table rows.
        $dv="";
        $dv2="";

        //if no values are defined use default weights from -10 up
        if(isset($shared['articleFields']['elements'][$k]['weight'])){
            $dv=$shared['articleFields']['elements'][$k]['weight'];
        }
        else $dv=$cc++;

        //if no value is defined use 0 for enabled value (unchecked) 
        if(isset($shared['articleFields']['elements'][$k]['enabled'])){
            $dv2=$shared['articleFields']['elements'][$k]['enabled'];
        }
        else $dv2=0;
        $rows[$k] = array(
            'data' =&gt; array(
                // Cell for the cross drag&amp;drop element.
                array('class' =&gt; array('entry-cross')),
                // Weight item for the tabledrag.
                array('data' =&gt; array(
                        '#type' =&gt; 'weight',
                        '#title' =&gt; t('Weight'),
                        '#title_display' =&gt; 'invisible',
                        '#default_value' =&gt; $dv,
                        '#attributes' =&gt; array(
                            'class' =&gt; array('entry-order-weight'),
                        ),
                    )),
                // Enabled checkbox.
                array('data' =&gt; array(
                        '#type' =&gt; 'checkbox',
                        '#title' =&gt; t('Enable'),
                        '#title_display' =&gt; 'invisible',
                        '#default_value' =&gt; t($dv2),
                    )),

                // Name textfield.
                array('data' =&gt; array(
                        '#type' =&gt; 'textfield',
                        '#size' =&gt; 50,
                        '#disabled' =&gt; TRUE,
                        '#title' =&gt; t('Field'),
                        '#title_display' =&gt; 'invisible',
                        '#default_value' =&gt; t($v),

                    )),

            ),
            'class' =&gt; array('draggable'),
        );


        // Build rows of the form elements in the table.
        $row_elements[$k] = array(
            'weight' =&gt; &amp;$rows[$k]['data'][1]['data'],
            'enabled' =&gt; &amp;$rows[$k]['data'][2]['data'],
            'field' =&gt; &amp;$rows[$k]['data'][3]['data'],
        );
    }
     //sort both the rows and the row elements on the weight value 

        uasort($rows, function($a, $b) {
            return $a['data'][1]['data']['#default_value'] &lt;=&gt; $b['data'][1]['data']['#default_value'];
        });


        uasort($row_elements, function($a, $b) {
            return $a['weight']['#default_value'] &lt;=&gt; $b['weight']['#default_value'];
        });



    $id=str_replace("shared_", "", $key);
    $id="form-item-shared-".$id."-articleFields";

    // add this to parrent form 
    $form[$key]['articleFields'] = array(
        '#prefix'=&gt;'&lt;div class='.$id.'&gt;',
        '#theme' =&gt; 'table',
        // The row form elements need to be processed and build,
        // therefore pass them as element children.
        'elements' =&gt; $row_elements,
        '#header' =&gt; array(
            // We need two empty columns for the weigth field and the cross.
            array('data' =&gt; NULL, 'colspan' =&gt; 2),
            t('Enabled'),
            t('Field'),
        ),
        '#rows' =&gt; $rows,
        '#empty' =&gt; t('There are no entries available.'),
        '#attributes' =&gt; array('id' =&gt; 'entry-order'),
        '#sufix'=&gt;'&lt;/div&gt;',
    );
    drupal_add_tabledrag('entry-order', 'order', 'sibling', 'entry-order-weight');